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

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

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

How many threads are spawned in parallelStream in Java 8?

The Oracle’s implementation[1] of parallel stream uses the current thread and in addition to that, if needed, also the threads that compose the default fork join pool ForkJoinPool.commonPool(), which has a default size equal to one less than the number of cores of your CPU. That default size of the common pool can be changed … Read more

CompletableFuture is not getting executed. If I use the ExecutorService pool it works as expected but not with the common ForkJoinPool

TL;DR: The ForkJoinPool uses daemon threads, whereas the ExecutorService is using non-daemon threads. The latter keep the JVM alive; the former do not. Also, the main thread is a non-daemon thread and when you block it waiting for the CompletableFuture to complete it remains alive (thus keeping the JVM alive). Daemon vs Non-Daemon Threads A … Read more

How to properly use Java Executor?

ExecutorService ExecutorService executor=Executors.newFixedThreadPool(50); It is simple and easy to use. It hides low level details of ThreadPoolExecutor. Prefer this one when number of Callable/Runnable tasks are small in number and piling of tasks in unbounded queue does not increase memory & degrade the performance of the system. If you have CPU/Memory constraints, use ThreadPoolExecutor with … Read more