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
211
votes
11 answers

Synchronization vs Lock

java.util.concurrent API provides a class called as Lock, which would basically serialize the control in order to access the critical resource. It gives method such as park() and unpark(). We can do similar things if we can use synchronized…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
132
votes
8 answers

Is there a Mutex in Java?

Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e)…
Noam Nevo
  • 3,021
  • 10
  • 35
  • 49
121
votes
2 answers

How many threads are spawned in parallelStream in Java 8?

In JDK8, how many threads are spawned when i'm using parallelStream? For instance, in the code: list.parallelStream().forEach(/** Do Something */); If this list has 100000 items, how many threads will be spawned? Also, do each of the threads get…
shashankg77
  • 2,206
  • 4
  • 18
  • 34
111
votes
3 answers

FixedThreadPool vs CachedThreadPool: the lesser of two evils

I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of…
105
votes
3 answers

WAITING at sun.misc.Unsafe.park(Native Method)

One of my applications hangs under some period of running under load, does anyone know what could cause such output in jstack: "scheduler-5" prio=10 tid=0x00007f49481d0000 nid=0x2061 waiting on condition [0x00007f494e8d0000] …
Konrad Pawlus
  • 1,641
  • 3
  • 14
  • 20
93
votes
4 answers

Java 8: Parallel FOR loop

I have heard Java 8 provides a lot of utilities regarding concurrent computing. Therefore I am wondering what is the simplest way to parallelise the given for loop? public static void main(String[] args) { Set servers = getServers(); …
87
votes
3 answers

Difference between shutdown and shutdownNow of Executor Service

I want to know the basic difference between shutdown() and shutdownNow() for shutting down the Executor Service? As far as I understood: shutdown() should be used for graceful shutdown which means all tasks that were running and queued for…
Geek
  • 26,489
  • 43
  • 149
  • 227
60
votes
5 answers

AtomicInteger.incrementAndGet() vs. AtomicInteger.getAndIncrement()

When return value is not of interest, is there any (even irrelevant in practice) difference between AtomicInteger.getAndIncrement() and AtomicInteger.incrementAndGet() methods, when return value is ignored? I'm thinking of differences like which…
hyde
  • 60,639
  • 21
  • 115
  • 176
59
votes
6 answers

Is ConcurrentHashMap totally safe?

this is a passage from JavaDoc regarding ConcurrentHashMap. It says retrieval operations generally do not block, so may overlap with update operations. Does this mean the get() method is not thread safe? "However, even though all operations are…
user697911
  • 10,043
  • 25
  • 95
  • 169
58
votes
7 answers

Is there java.util.concurrent equivalent for WeakHashMap?

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? Collections.synchronizedMap(new WeakHashMap()); i.e. is there something from java.util.concurrent one…
Nikita
  • 6,019
  • 8
  • 45
  • 54
57
votes
1 answer

scala.concurrent.blocking - what does it actually do?

I have spent a while learning the topic of Scala execution contexts, underlying threading models and concurrency. Can you explain in what ways does scala.concurrent.blocking "adjust the runtime behavior" and "may improve performance or avoid…
matanster
  • 15,072
  • 19
  • 88
  • 167
51
votes
4 answers

Do we need to make ConcurrentHashMap volatile?

We have a shared ConcurrentHashMap which is read and written by 2 threads. class Test { private ConcurrentHashMap map = new ConcurrentHashMap<>(); Object read() { return map.get(object); } void write(Object…
user3739844
  • 705
  • 1
  • 5
  • 7
48
votes
4 answers

ExecutorCompletionService? Why do need one if we have invokeAll?

If we use an ExecutorCompletionService we can submit a series of tasks as Callables and get the result interacting with the CompletionService as a queue. But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
46
votes
2 answers

@GuardedBy , @ThreadSafe ,@NotThreadSafe

I see that the above annotations are used extensively in the book JCIP . I think it is really useful because even in the absence of proper documentation it says some things about the synchronization policies . I also see that Intellij Idea makes use…
Inquisitive
  • 7,476
  • 14
  • 50
  • 61
43
votes
3 answers

Thread safe Hash Map?

I am writing an application which will return a HashMap to user. User will get reference to this MAP. On the backend, I will be running some threads which will update the Map. What I have done so far? I have made all the backend threads so share a…
user381878
  • 1,543
  • 5
  • 17
  • 30
1
2 3
93 94