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
25
votes
2 answers

Behavior of entrySet().removeIf in ConcurrentHashMap

I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm…
25
votes
2 answers

Why java.util.concurrent.atomic.AtomicBoolean is internally implemented with int?

AtomicBoolean stores its value in: private volatile int value; Then, for example, extracting its value is done like this: public final boolean get() { return value != 0; } What is the reason behind it? Why boolean was not used?
Marcin
  • 4,080
  • 1
  • 27
  • 54
24
votes
3 answers

Why does this CompletableFuture work even when I don't call get() or join()?

I had a question while studying CompletableFuture. The get()/join() methods are blocking calls. What if I don't call either of them? This code calls get(): // Case 1 - Use get() CompletableFuture.runAsync(() -> { try { …
24
votes
5 answers

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

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…
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
22
votes
5 answers

How to Block and wait using AtomicBoolean

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…
Ben
  • 1,086
  • 3
  • 15
  • 30
22
votes
2 answers

ExecutorService awaitTermination gets stuck

I made a fixed size thread pool with Executors.newFixedThreadPool(2), and I executed 10 Runnable objects. I set breakpoints and traced through the execution. However, fixedSizeThreadPool.awaitTermination() does not allow me to continue even though…
Kevin
  • 1,080
  • 3
  • 15
  • 41
22
votes
2 answers

Is having a single threadpool better design than multiple threadpools

What are the advantages and disadvantages of having more than one threadpool in Java? I have seen code where there are multiple threadpools for different "types" of tasks, and I'm not sure whether its better design or just developers being lazy.…
22
votes
2 answers

ExecutorService.invokeAll does NOT support collection of runnable task

Wanted to run collection of Runnable task through invokeAll(..) method of ExecutorService. But that's not supported as of now (supports collection of Callable task only) Any specific reason for this? What's the alternative to do something similar.
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
22
votes
3 answers

Is an executor meant to be reused?

Is an executor object meant to be reused after a shutdown? I mean if I call shutdown or shutdownNow after the executor is terminated, should I do new to create a new thread pool or is it possible to somehow "reset"/reuse the previously terminated…
Jim
  • 18,826
  • 34
  • 135
  • 254
22
votes
4 answers

lock.lock() before try

Is there any difference between: private Lock lock = new ReentrantLock(true); public void getIn (int direction) throws InterruptedException { lock.lock(); try { ... and ... public void getIn (int direction) throws…
9628001
  • 509
  • 1
  • 4
  • 11
21
votes
2 answers

ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

Java 8 introduced new way to obtain a concurrent Set implementation // Pre-Java-8 way to create a concurrent set Set oldStyle = Collections.newSetFromMap(new ConcurrentHashMap<>()); // New method in Java 8 Set newStyle =…
turbanoff
  • 2,439
  • 6
  • 42
  • 99
21
votes
6 answers

Why cannot run() of Runnable throw checked Exceptions?

According to section 6.3.2 of JCIP : Runnable is a fairly limiting abstraction; run can not return a value or throw checked exception . run() can not return a value since its return type is void but why can not it throw a checked exception ?
Inquisitive
  • 7,476
  • 14
  • 50
  • 61
20
votes
3 answers

When is CopyOnWriteArraySet useful to achieve thread-safe HashSet?

In Java, there is thread-safe version HashMap named ConcurrentHashMap and thread-safe version TreeMap named ConcurrentSkipListMap, but there is no ConcurrentHashSet for HashSet. Instead, there are usually 4 ways to use thread-safe Set: Set
coderz
  • 4,847
  • 11
  • 47
  • 70
20
votes
7 answers

How to do a lazy create and set with AtomicReference in a safe and efficient manner?

I'm looking to lazily create something and cache the results as an optimization. Is the code below safe and efficient, or is there a better way to do this? Is the compare and set loop needed here? ... AtomicReference fCachedValue = new…
marathon
  • 7,881
  • 17
  • 74
  • 137
20
votes
3 answers

java.util.ConcurrentModificationException android after remove elements from array list

I have the folloing code in my android app: /** * callback executed after fetching the data. */ public void OnPointsFetch(ArrayList result) { toggleLoader(false); this.shops = result; if(activeFilter ==…
brpaz
  • 3,618
  • 9
  • 48
  • 75
1 2
3
93 94