Questions tagged [java.util.concurrent]

Java package which contains utility classes commonly useful in concurrent programming. This package includes a few small standardized extensible frameworks, as well as some classes that provide useful functionality and are otherwise tedious or difficult to implement.

java.util.concurrent is a core Java package which contains utility classes commonly useful in concurrent programming. This package includes a few small standardized extensible frameworks, as well as some classes that provide useful functionality and are otherwise tedious or difficult to implement.

Main components include:

  • Executors
  • Queues
  • Timing
  • Synchronizers, Semaphores, CountDownLatch
  • Concurrent Collections

More information is available at the JavaDoc description

1396 questions
19
votes
2 answers

Why does java.util.concurrent.RunnableFuture have a run() method?

As I was going through JDK 7, I found that java.util.concurrent.RunnableFuture has a run method. I wonder what the significance of duplicating the same run method signature in the interface is when it already extends Runnable. package…
Maas
  • 1,317
  • 9
  • 20
18
votes
2 answers

Is java.util.concurrent.Future threadsafe?

I am trying to find documentation indicating if java.util.concurrent.Future is/is not threadsafe. Eg can I safely give the same instance of Future to multiple threads, which will all call Future.get(...)? I have tested code using Future in this…
S42
  • 1,061
  • 2
  • 10
  • 16
18
votes
1 answer

Confused by jcstress test on ReentrantReadWriteLock#tryLock failing

I am trying to get to grips with JCStress. To ensure I understand it, I decided to write some simple tests for something that I know must be correct: java.util.concurrent.locks.ReentrantReadWriteLock. I wrote some very simple tests to check lock…
adamretter
  • 3,885
  • 2
  • 23
  • 43
18
votes
3 answers

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

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…
cpz
  • 1,002
  • 2
  • 12
  • 27
18
votes
3 answers

Can I use the work-stealing behaviour of ForkJoinPool to avoid a thread starvation deadlock?

A thread starvation deadlock occurs in a normal thread pool if all the threads in the pool are waiting for queued tasks in the same pool to complete. ForkJoinPool avoids this problem by stealing work from other threads from inside the join() call,…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
18
votes
5 answers

Long primitive or AtomicLong for a counter?

I have a need for a counter of type long with the following requirements/facts: Incrementing the counter should take as little time as possible. The counter will only be written to by one thread. Reading from the counter will be done in another…
Rich
  • 15,602
  • 15
  • 79
  • 126
18
votes
6 answers

How do I "cancel" a CountDownLatch?

I have multiple consumer threads waiting on a CountDownLatch of size 1 using await(). I have a single producer thread that calls countDown() when it successfully finishes. This works great when there are no errors. However, if the producer detects…
emmby
  • 99,783
  • 65
  • 191
  • 249
17
votes
6 answers

Concurrent Map with fixed size

I need a map with the following requirements : It should be highly concurrent. The put(), get() and remove() methods may be called by multiple threads simultaneously. It should be of fixed size. If the size of the HashMap reaches to the max value…
17
votes
2 answers

How does ConcurrentHashMap handle rehashing?

I am wondering how does ConcurrentHashMap handle rehashing while another thread is still writing on another segment/partition. As far as I understand, ConcurrentHashMap locks the segment independently, so for example: If Thread1 writes to the…
peter
  • 8,333
  • 17
  • 71
  • 94
17
votes
1 answer

ScheduledExecutorService vs Timer vs Handler

What are the pros and cons of using ScheduledExecutorService / Timer / Handler? As I understand in Android instead of Timer it's need to use Handler, but what about ScheduledExecutorService? As I understand Handler and ScheduledExecutorService is…
pvllnspk
  • 5,667
  • 12
  • 59
  • 97
16
votes
2 answers

Difference between Executor and ExecutorCompletionservice in java

As the question title itself says what is the difference between Executors and ExecutorCompletionService classes in java? I am new to the Threading,so if any one can explain with a piece of code, that would help a lot.
Dipesh Gupta
  • 787
  • 1
  • 7
  • 20
16
votes
2 answers

Why are ConcurrentSkipListSet ascending Iterators 'faster' than descending ones?

I’m using the descendingIterator method on ConcurrentSkipListSet. I’ve just checked the documentation and noticed the following comment: ‘Ascending ordered views and their iterators are faster than descending ones.’ See…
GoldenJam
  • 1,459
  • 1
  • 11
  • 17
16
votes
2 answers

Atomic variables vs synchronized methods

I have a counter class which has increment and decrement method, both the methods are synchronized. public class Counter { int count = 0; public synchronized void increment(){ count++; } public synchronized void decrement(){ …
Kuldeep
  • 599
  • 11
  • 28
16
votes
4 answers

future.cancel does not work

I have a nice and compact code, which does not work as I expected. public class Test { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { try { …
teglus
  • 163
  • 1
  • 1
  • 4
16
votes
2 answers

what's the difference between CyclicBarrier/CountDownLatch and join in Java?

what's the difference between CyclicBarrier/CountDownLatch and join in Java? What's the advantage of CyclicBarrier and CountDownLatch? In my opinion, just use join we can wait for a thread complete its execution.
coderz
  • 4,847
  • 11
  • 47
  • 70