ExecutorCompletionService? Why do need one if we have invokeAll?

Using a ExecutorCompletionService.poll/take, you are receiving the Futures as they finish, in completion order (more or less). Using ExecutorService.invokeAll, you do not have this power; you either block until are all completed, or you specify a timeout after which the incomplete are cancelled. static class SleepingCallable implements Callable<String> { final String name; final long period; … Read more

Java 8: Parallel FOR loop

Read up on streams, they’re all the new rage. Pay especially close attention to the bit about parallelism: “Processing elements with an explicit for-loop is inherently serial. Streams facilitate parallel execution by reframing the computation as a pipeline of aggregate operations, rather than as imperative operations on each individual element. All streams operations can execute … Read more

Is having a single threadpool better design than multiple threadpools

The purpose of having separate dedicated threadpools is so that an activity doesn’t get starved for threads because other activities took all the threads. If some service has its own threadpool then it is assured of having a certain number of threads at its disposal and it’s not as sensitive to demands made by other … Read more

Whether to use invokeAll or submit – java Executor service

Option 1 : You are submitting the tasks to ExecutorService and you are not waiting for the completion of all tasks, which have been submitted to ExecutorService Option 2 : You are waiting for completion of all tasks, which have been submitted to ExecutorService. What should be the preferred way? Depending on application requirement, either … Read more

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

unsafe.park is pretty much the same as thread.wait, except that it’s using architecture specific code (thus the reason it’s ‘unsafe’). unsafe is not made available publicly, but is used within java internal libraries where architecture specific code would offer significant optimization benefits. It’s used a lot for thread pooling. So, to answer your question, all … Read more

FixedThreadPool vs CachedThreadPool: the lesser of two evils

A CachedThreadPool seems appropriate for your situation as there are no negative consequence to using one for long running threads directly. The comment in the java doc about CachedThreadPools being suitable for short tasks merely suggest that they are particularly appropriate for such cases, not that they cannot be used for long running tasks. The … Read more

Run Java Threads sequentially

You could use Executors.newSingleThreadExecutor(), but strictly speaking this launches only one Thread, so may not be expected solution. The simpliest solution using just Thread class: Thread1.start(); Thread1.join(); Thread2.start(); Thread2.join(); Thread3.start(); Thread3.join(); (I omitted exception handling for clarity, Thread.join() can throw InterruptedException)

What is adaptive spinning w.r.t lock acquisition?

What exactly is adaptive spinning? To quote from this Java 6 performance page: Adaptive spinning is an optimization technique where a two-phase spin-then-block strategy is used by threads attempting a contended synchronized enter operation. This technique enables threads to avoid undesirable effects that impact performance such as context switching and repopulation of Translation Lookaside Buffers … Read more

Is ConcurrentHashMap totally safe?

The get() method is thread-safe, and the other users gave you useful answers regarding this particular issue. However, although ConcurrentHashMap is a thread-safe drop-in replacement for HashMap, it is important to realize that if you are doing multiple operations you may have to change your code significantly. For example, take this code: if (!map.containsKey(key)) return … Read more