java.util.concurrent

Future cancel method documentation

£可爱£侵袭症+ 提交于 2019-12-05 18:47:33
According to http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html isDone returns true if cancel(boolean mayInterruptIfRunning) was called. After this method returns, subsequent calls to isDone() will always return true. However, it is possible that task is running and mayInterruptIfRunning is set to false . So, what should return isDone() right after that call? true because of cancel (which is wrong)? Also, it's not clear whether cancel(boolean) method returns false . P. S. I'm implementing some simple thread pool, so I'm inheriting from Future . After cancel(...) , isDone

How to make a ScheduledExecutorService terminate automatically when its scheduled task is cancelled

帅比萌擦擦* 提交于 2019-12-05 14:24:20
I'm using a ScheduledExecutorService to close a network connection if it has been open for more than several hours. In most cases however, the network connection is closed before the timeout is reached, so I cancel the ScheduledFuture . In this case, I also want the executor service to terminate and to release its thread pool. To my surprise, this does not work out of the box: Although I have called shutdown() on the executor service after scheduling the task, the executor service does not terminate automatically when its only scheduled task is cancelled. From the JavaDoc of ExecutorService

How to Block and wait using AtomicBoolean

北战南征 提交于 2019-12-05 11:46:31
问题 I am looking for a way of pausing a Thread. I started with affectively using a boolean flag (called 'paused'), and wrapping a check with a while loop (pause). Within the while loop there’s a Thread.wait() to block the execution. I’ve been looking at the AtomicBoolean , which seems to do the trick apart from it doesn’t block. Is there a alternative or extended version of AtomicBoolean that has a block method ? i.e. something like AtomicBoolean.getFalse() of AtomoicBoolean.get(false) ? They

Are read and write locks in ReentrantReadWriteLock somehow related?

天涯浪子 提交于 2019-12-05 10:27:11
Please explain me more the contract. I can't figure out if two locks contained in ReentrantReadWriteLock somehow related? Or these are just a bundle of two normal locks? If threads are waiting Read Lock it is shared but when thread wants to acquire write lock only that thread is allowed the access same as mutual exclusion. So either one of operation is allowed . if lock is held by readers and thread request write lock no more readers are allowed to acquire read lock until thread which has acquired write lock release it . It allows multiple threads to read a resource concurrently, but requires

Clever asynchronous repaint in Java

你说的曾经没有我的故事 提交于 2019-12-05 05:12:53
I have a use-case coming from a GUI problem I would like to submit to your sagacity. Use case I have a GUI that displays a computation result depending on some parameters the user set in a GUI. For instance, when the user moves a slider, several events are fired, that all trigger a new computation. When the user adjust the slider value from A to B, a dozens of events are fired. But the computation can take up to several seconds, whereas the slider adjustment can fire an event every few 100 ms. How to write a proper Thread that would listen to these events, and kind of filter them so that the

Is it not possible to supply a thread facory or name pattern to ForkJoinPool?

那年仲夏 提交于 2019-12-05 03:21:53
I would like to set name for threads of the ForkJoinPool used by work stealing pool, supplied by ExecutorService newWorkStealingPool(int parallelism) or ExecutorService newWorkStealingPool() So far I could not find a way to set custom names on threads used by this ExecutorService , is there a way? newWorkStealingPool() basically supplies a ForkJoinPool , but ForkJoinPool also doesn't have a public constructor with supplied name pattern. update : I have now found this constructor of ForkJoinPool which takes a thread factory ForkJoinPool.ForkJoinWorkerThreadFactory . But factory should return a

What's the different between LinkedBlockingQueue and ConcurrentLinkedQueue?

一世执手 提交于 2019-12-05 01:46:21
问题 I've read the blog, but i'm not sure whether his conclusion is correct : http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html#ixzz1seaiSLwp He said : As you can see from the provided performance results LinkedBlockingQueue achieved the best combined (adding and removing elements) performance results and should be your number one candidate for implementing producer – consumer schenarios. I wonder that, doen't it faster if i don't use lock in my code ? So why the

Java Remove Specific Item From ConcurrentHashMap

柔情痞子 提交于 2019-12-05 01:22:47
Is using the remove() method okay? I've read an article that synchronization hasn't been added to the remove method. How do I properly remove a specific item from a ConcurrentHashMap? Example Code: ConcurrentHashMap<String,Integer> storage = new ConcurrentHashMap<String,Integer>(); storage.put("First", 1); storage.put("Second", 2); storage.put("Third",3); //Is this the proper way of removing a specific item from a tread-safe collection? storage.remove("First"); for (Entry<String, Integer> entry : storage.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // ... System

What is the purpose of ScheduledFuture.get() method if is retrieved from the scheduleWithFixedDelay/scheduleAtFixedRate method

蹲街弑〆低调 提交于 2019-12-04 23:43:19
I am confused with the following I know, if I use the schedule method from the ScheduledThreadPoolExecutor class: ScheduledFuture<?> scheduledFuture = scheduledThreadPoolExecutor.schedule(myClassRunnable, 5, TimeUnit.SECONDS); I am able to retrieve later the value through scheduledFuture.get(5, TimeUnit.SECONDS) or scheduledFuture.get() and should be null because the task has been executed just only once and it is completed. And null because I am working with the shedule's Runnable method version and not with the shedule's Callable method version. It according with the API Until here I am fine

Java example of using ExecutorService and PipedReader/PipedWriter (or PipedInputStream/PipedOutputStream) for consumer-producer

落爺英雄遲暮 提交于 2019-12-04 21:46:51
I'm looking for a simple producer - consumer implementation in Java and don't want to reinvent the wheel I couldn't find an example that uses both the new concurrency package and either of the Piped classes Is there an example for using both PipedInputStream and the new Java concurrency package for this? Is there a better way without using the Piped classes for such a task? For your task it might be sufficient to just use a single thread and write to the file using a BufferedOutputStream as you are reading from the database. If you want more control over the buffer size and the size of chunks