java.util.concurrent

AtomicBoolean, set flag once, necessary? Might a static boolean be ok?

前提是你 提交于 2019-12-20 05:50:37
问题 I am setting a flag which is set once by any thread that get to set it. All other threads will at various time, pretty often read this flag repeateadly. Right now I am using an AtomicBoolean, which works fine, but I know that if it is queried quite often it can be considerably slower than plain boolean, ( not sure if this is true ). Would it be thread safe to instead change this to a static boolean? Set the flag to true by whoever gets to do that, in fact all of them might be allowed to set

Java ServiceExecutor terminating condition

怎甘沉沦 提交于 2019-12-20 03:42:14
问题 I'm new to java executor stuff. I'm using Java's ExecutorService to launch several threads to process data. Executor executor = Executors.newFixedThreadPool(poolSize); for(int i=0; i< 5;i++) executor.execute(new MyRunnable(i)); once the threads don't find data, they gracefully terminate. My question is what happens to the Executor when all the threads terminate, is it still running its master thread ? or it will terminate itself and whole application will finish gracefully? in case executor

JavaFX thread synchronization with Java thread

丶灬走出姿态 提交于 2019-12-20 03:03:18
问题 Is there a way to synchronize a JavaFX Platform thread and a standard Java thread? Currently, when triggered, the JavaFX thread fires before the standard Java thread has finished adding all of the images to the observable list, and so the imageList is updated with a blank collection. private final TilePane imageList; final File[] files = new File(dir).listFiles(); final List<ImageView> views = FXCollections.observableArrayList(); new Thread() { @Override public void run() { for (final File

What is happening underneath the Future.cancel(true)

我的梦境 提交于 2019-12-20 02:32:55
问题 Suppose I have a Runnable instance: class MyTask implements Runnable { public void run() { //some heavy calculation which takes time Thread.sleep(5000) //rest code ... } } Then, I use ExecutorService to submit the above task: ExecutorService service = Executors.newFixedThreadPool(3); Future<?> task = service.submit(new MyTask()); Now, I can cancel the task by task.cancel(true); . What I have understood is that the task.cancel(true) will interrupt the working thread in which this task is

Restricting thread count and Java concurrency

孤者浪人 提交于 2019-12-19 09:55:12
问题 I couldn't find an example of this specific case using the latest JAVA concurrent routines. I plan to use threads to process items from an open queue which may contain 0 to thousands requests. I want to restrict so at at any given time there be no less than 0 and no more than say 10 threads handling queue items. Is there a Java concurrent process geared towards this specific type of case? 回答1: I think a thread pool is what you are looking for. Take a look at ExecutorService and Executors.

Restricting thread count and Java concurrency

蓝咒 提交于 2019-12-19 09:52:52
问题 I couldn't find an example of this specific case using the latest JAVA concurrent routines. I plan to use threads to process items from an open queue which may contain 0 to thousands requests. I want to restrict so at at any given time there be no less than 0 and no more than say 10 threads handling queue items. Is there a Java concurrent process geared towards this specific type of case? 回答1: I think a thread pool is what you are looking for. Take a look at ExecutorService and Executors.

Java threads slow down towards the end of processing

女生的网名这么多〃 提交于 2019-12-19 09:06:27
问题 I have a Java program that takes in a text file containing a list of text files and processes each line separately. To speed up the processing, I make use of threads using an ExecutorService with a FixedThreadPool with 24 threads. The machine has 24 cores and 48GB of RAM. The text file that I'm processing has 2.5 million lines. I find that for the first 2.3 million lines or so things run very well with high CPU utilization. However, beyond some point (at around the 2.3 lines), the performance

Why aren't Java.util.concurrent.TimeUnit types greater than SECONDS available in Android?

时光总嘲笑我的痴心妄想 提交于 2019-12-19 05:50:46
问题 I miss MINUTES, HOURS, DAYS , which exist in documentaion since API level 1 (I use 7th or 2.1 version for the application). I have read this question, where this miss was also pointed out (though, it wasn't in the question itself), but as solution making own calculations was only proposed. I am not lazy, but I send some data to server (Java project), where TimeUnit.MINUTES or TimeUnit.HOURS are actively used. Just want to avoid mistakes. Will be grateful for any help. 回答1: If you open the

Scalable way to access every element of ConcurrentHashMap<Element, Boolean> exactly once

こ雲淡風輕ζ 提交于 2019-12-19 04:04:52
问题 I have 32 machine threads and one ConcurrentHashMap<Key,Value> map , which contains a lot of keys. Key has defined a public method visit() . I want to visit() every element of map exactly once using the processing power I have available and possibly some sort of thread pooling. Things I could try: I could use the method map.keys() . The resulting Enumeration<Key> could be iterated over using nextElement() , but since a call to key.visit() is very brief I won't manage to keep threads busy. The

Strange code in java.util.concurrent.LinkedBlockingQueue

心已入冬 提交于 2019-12-18 13:16:35
问题 All! I found strange code in LinkedBlockingQueue: private E dequeue() { // assert takeLock.isHeldByCurrentThread(); Node<E> h = head; Node<E> first = h.next; h.next = h; // help GC head = first; E x = first.item; first.item = null; return x; } Who can explain why do we need local variable h? How can it help for GC? 回答1: To better understand what happens let's see how the list looks like after executing the code. First consider an initial list: 1 -> 2 -> 3 Then h points to head and first to h