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
42
votes
4 answers

Memory Consistency - happens-before relationship in Java

While reading Java docs on Memory Consistency errors. I find points related to two actions that creates happen - before relationship: When a statement invokes Thread.start(), every statement that has a happens-before relationship with that…
Prateek
  • 12,014
  • 12
  • 60
  • 81
37
votes
8 answers

How to give name to a callable Thread?

I am executing a Callable Object using ExecutorService thread pool. I want to give a name to this thread. To be more specific, in older version I did this - Thread thread = new Thread(runnable Task); thread.setName("My Thread Name"); I use thread…
user381878
  • 1,543
  • 5
  • 17
  • 30
35
votes
3 answers

Implementation of BlockingQueue: What are the differences between SynchronousQueue and LinkedBlockingQueue

I see these implementation of BlockingQueue and can't understand the differences between them. My conclusion so far: I won't ever need SynchronousQueue LinkedBlockingQueue ensures FIFO, BlockingQueue must be created with parameter true to make it…
nanda
  • 24,458
  • 13
  • 71
  • 90
34
votes
5 answers

Hystrix command fails with "timed-out and no fallback available"

I've noticed that some of the commands in my application fail with Caused by: ! com.netflix.hystrix.exception.HystrixRuntimeException: GetAPICommand timed-out and no fallback available. out: ! at…
dkulkarni
  • 2,780
  • 4
  • 27
  • 36
32
votes
3 answers

Is there BlockingMap as BlockingQueue in java?

I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of BlockingMap to wait there until the corresponding key is…
zjffdu
  • 25,496
  • 45
  • 109
  • 159
29
votes
3 answers

Difference between Semaphore initialized with 1 and 0

Please tell what is difference between a Semaphore initialized with 1 and Vs. intialized zero, as below: public static Semaphore semOne = new Semaphore(1); and public static Semaphore semZero = new Semaphore(0);
Atul Kumar
  • 719
  • 3
  • 8
  • 29
28
votes
3 answers

Why is Java Future.get(timeout) Not Reliable?

Future.get(timeout) does not reliably throw the TimeoutException after the given timeout. Is this normal behavior or can I do something to make this more reliable? This test fails on my machine. However if I sleep for 3000 instead of 2000, it will…
Andrew Raphael
  • 507
  • 2
  • 7
  • 9
28
votes
6 answers

Why Lock condition await must hold the lock

I am in doubt with that , in Java language, we need to acquire the lock, before we await some condition to be satisfied. For example, int java monitor lock: synchronized(lock){ System.out.println("before lock ..."); lock.wait(); …
Chinaxing
  • 8,054
  • 4
  • 28
  • 36
28
votes
4 answers

Can I use Callable threads without ExecutorService?

Can I use Callable threads without ExecutorService? We can use instances of Runnable and subclasses of Thread without ExecutorService and this code works normally. But this code works consistently: public class Application2 { public static…
28
votes
3 answers

Need simple explanation how "lock striping" works with ConcurrentHashMap

According to Java Concurrency in Practice, chapter 11.4.3 says: Lock splitting can sometimes be extended to partition locking on a variablesized set of independent objects, in which case it is called lock striping. For example, the…
GedankenNebel
  • 2,437
  • 5
  • 29
  • 39
27
votes
2 answers

collecting from parallel stream in java 8

I want to take an input and apply parallel stream on that, then I want output as list. Input could be any List or any collection on which we can apply streams. My concerns here is that if we want output as map them we have an option from java is…
Vip
  • 1,448
  • 2
  • 17
  • 20
27
votes
3 answers

Whether to use invokeAll or submit - java Executor service

I have a scenario where I have to execute 5 thread asynchronously for the same callable. As far as I understand, there are two options: 1) using submit(Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List>…
Ankit
  • 3,083
  • 7
  • 35
  • 59
27
votes
2 answers

CompletableFuture from Callable?

Today I experimented with the "new" CompletableFuture from Java 8 and found myself confused when I didn't find a runAsync(Callable) method. I can do it myself like shown below, but why is this (to me very obvious and useful utility method) missing?…
diesieben07
  • 1,487
  • 1
  • 14
  • 25
26
votes
5 answers

Thread-safe implementation of max

I need to implement global object collecting statistics for web server. I have Statistics singleton, which has method addSample(long sample), which subsequently call updateMax. This has to be obviously thread-safe. I have this method for updating…
Tibor Blenessy
  • 4,254
  • 29
  • 35
26
votes
1 answer

CompletableFuture, mutable objects and memory visibility

I'm trying to understand how CompletableFuture in Java 8 interacts with the Java memory model. It seems to me that for programmer sanity, the following should ideally hold true: Actions in the thread that completes a CompletableFuture happen-before…
1
2
3
93 94