concurrency

Run java thread at specific times

筅森魡賤 提交于 2020-01-20 05:56:05
问题 I have a web application that synchronizes with a central database four times per hour. The process usually takes 2 minutes. I would like to run this process as a thread at X:55, X:10, X:25, and X:40 so that the users knows that at X:00, X:15, X:30, and X:45 they have a clean copy of the database. It is just about managing expectations. I have gone through the executor in java.util.concurrent but the scheduling is done with the scheduleAtFixedRate which I believe provides no guarantee about

What is the preferred way to modify a value in ConcurrentHashMap?

六月ゝ 毕业季﹏ 提交于 2020-01-20 04:14:48
问题 Let's say I have a Concurrent Map that is high-read, low-write, and needs to store application data: ConcurrentMap<UUID, Data> map = new ConcurrentHashMap<UUID, Data>(); Then, during startup and through user input, data is added to the map: public void createData(Data newData) { map.put(newId, newData); // etc... } If I then need to change the data, should I: A) Make the Data class objects immutable, and then conduct a put operation every time a change is needed for a Data object: public void

how to make an application thread safe?

烂漫一生 提交于 2020-01-19 18:20:54
问题 I thought thread safe, in particular, means it must satisfy the need for multiple threads to access the same shared data. But, it seems this definition is not enough. Can anyone please list out the things to be done or taken care of to make an application thread safe . If possible, give an answer with respect to C/C++ language. 回答1: There are several ways in which a function can be thread safe. It can be reentrant . This means that a function has no state, and does not touch any global or

Concurrent byte array access in Java with as few locks as possible

依然范特西╮ 提交于 2020-01-19 15:23:13
问题 I'm trying to reduce the memory usage for the lock objects of segmented data. See my questions here and here. Or just assume you have a byte array and every 16 bytes can (de)serialize into an object. Let us call this a "row" with row length of 16 bytes. Now if you modify such a row from a writer thread and read from multiple threads you need locking. And if you have a byte array size of 1MB (1024*1024) this means 65536 rows and the same number of locks. This is a bit too much, also that I

FileInputStream and FileOutputStream to the same file: Is a read() guaranteed to see all write()s that “happened before”?

血红的双手。 提交于 2020-01-19 11:20:20
问题 I am using a file as a cache for big data. One thread writes to it sequentially, another thread reads it sequentially. Can I be sure that all data that has been written (by write() ) in one thread can be read() from another thread, assuming a proper "happens-before" relationship in terms of the Java memory model? Is this behavior documented? In my JDK, FileOutputStream does not override flush() , and OutputStream.flush() is empty. That's why I'm wondering... The streams in question are owned

FileInputStream and FileOutputStream to the same file: Is a read() guaranteed to see all write()s that “happened before”?

无人久伴 提交于 2020-01-19 11:16:40
问题 I am using a file as a cache for big data. One thread writes to it sequentially, another thread reads it sequentially. Can I be sure that all data that has been written (by write() ) in one thread can be read() from another thread, assuming a proper "happens-before" relationship in terms of the Java memory model? Is this behavior documented? In my JDK, FileOutputStream does not override flush() , and OutputStream.flush() is empty. That's why I'm wondering... The streams in question are owned

how to process several commands concurrently and continue the rest only after all is done on ubuntu

倖福魔咒の 提交于 2020-01-17 06:19:21
问题 To be specific, I have two command need to be run in shell on Ubuntu at the same time , like command_A and command_B . And I have some other commands need to be run only after command_A and command_B has finished, named as command_rest . In addition, command_A and command_B run in separate terminals and when they are finished they can close themselves. This maybe need techniques related to signal and wait and gnome-terminal i guess, but i cannot find a solution. 回答1: You can do it like this

how to process several commands concurrently and continue the rest only after all is done on ubuntu

假装没事ソ 提交于 2020-01-17 06:19:11
问题 To be specific, I have two command need to be run in shell on Ubuntu at the same time , like command_A and command_B . And I have some other commands need to be run only after command_A and command_B has finished, named as command_rest . In addition, command_A and command_B run in separate terminals and when they are finished they can close themselves. This maybe need techniques related to signal and wait and gnome-terminal i guess, but i cannot find a solution. 回答1: You can do it like this

ScheduledExecutorService and ThreadPoolTaskExecutor that interrupts tasks after a timeout

元气小坏坏 提交于 2020-01-17 03:44:14
问题 I Used ExecutorService that interrupts tasks after a timeout.I use a ScheduledExecutorService for this. First I submitted the thread and it once to begin immediately and retain the future that is created. After that i use ScheduledExecutorService as a new task that would cancel the retained future after some period of time. //Start Spring executor to submit tasks ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) ApplicationContextProvider.getApplicationContext().getBean(

wait() and notify() method , always IllegalMonitorStateException is happen and tell me current Thread is not Owner Why?

好久不见. 提交于 2020-01-16 16:08:23
问题 package pkg_1; public class ExpOnWaitMethod extends Thread { static Double x = new Double(20); public static void main(String[] args) { ExpOnWaitMethod T1 = new ExpOnWaitMethod(); ExpOnWaitMethod T2 = new ExpOnWaitMethod(); T1.start(); T2.start(); } public void run() { Mag mag = new Mag(); synchronized (x) { try { for (int i = 1; i < 10; i++) { mag.nop(Thread.currentThread()); x = i * 2.0; } } catch (InterruptedException e) { e.printStackTrace(); } } } } class Mag { char ccc = 'A'; public