Concurrency

Java Concurrency Interview Questions

Java concurrency interview guide lists frequently asked questions for beginners and experienced programmers to help refresh multi-threading concepts.

Daemon Threads in Java

Learn what is a daemon thread in Java, how to create a daemon thread, and various methods present for daemon threads in the Thread class.

Java Semaphore vs ReentrantLock

Learn what a Semaphore and a Reentrant Lock are along with practical examples. We will also explore some of the main differences between the two and the use cases where we can use these while working in a multithreaded application. 1. What is a Semaphore? A Semaphore is a Thread …

Guide to Join Threads in Java

Learn the need and methods to join two threads in Java with examples. Learn to set wait expiration time and avoid thread deadlock conditions.

Guide to Java Concurrency

The backbone of Java concurrency are threads. A thread is a lightweight process with its own call stack but can access shared data of others.

Java Virtual Threads: Project Loom

In Java, Virtual threads (JEP-425) are JVM managed light-weight threads that will help in writing high throughput concurrent applications.

How to Shutdown a Java ExecutorService

Learn ExecutorService shutdown(), shutdownNow() and awaitTermination​() APIs and how to use them correctly to shutdown executor under different conditions,

Java ExecutorService invokeAll()

Learn to run multiple Callable tasks with ExecutorService.invokeAll(tasks) API and processing all the results returned from tasks in form of Future class instances in this ExecutorService Callable example.

ExecutorService invokeAny()

Learn to use ExecutorService invokeAny() method where we execute multiple tasks at same time, but we make a decision asap any one of tasks is completed.

Executor RejectedExecutionHandler

Learn to handle tasks which are submitted to Executor and are rejected because the executor has been shutdown for any reason using RejectedExecutionHandler. 1. When tasks get rejected Remember when we finish the execution of an executor, we use the shutdown() method. The executor waits for the completion of tasks …

Guide to ExecutorService in Java

Learn to use Java ExecutorService to execute a Runnable or Callable class in an asynchronous way. Also learn the various best practices to utilize it in most efficient manner in any Java application.

Guide to AtomicInteger in Java

The AtomicInteger class protects an underlying int value by providing methods that perform atomic operations on the value. It shall not be used as a replacement for an Integer class.

Java Hashtable class

Java Hashtable class is an implementation of hash table data structure. It is very much similar to HashMap but it is synchronized while HashMap is not.

Producer Consumer Problem Using BlockingQueue

BlockingQueue is excellent when you want to skip the complexity involved in wait–notify statements. This BlockingQueue can be used to solve the producer-consumer problem as well as given blow example. As this problem is well known to every programmer, I am not going in detail of problem description. How BlockingQueue …

Java Thread Life Cycle and States

Learn about the lifecycle of a Thread in Java, and how the state transitions happen between different states of the Thread by JVM and OS.

Java Multi-threading Evolution and Topics

One of our reader, Anant, asked this extremely good question to elaborate / list down all related topics that we should know about multi-threading including changes made in java 8.( Beginner level to Advance level). All he wanted to know was evolution of Multi-threading Framework in Java from Simple Runnable …

Java Callable Future Example

One of the benefits of the Java executor framework is that we can run concurrent tasks that may return a single result after processing the tasks. The Java Concurrency API achieves this with the following two interfaces Callable and Future. In this tutorial, we will learn to execute Callable tasks …

Java Fixed Size Thread Pool Executor Example

In previous tutorial, we learned about basic thread pool executor with unlimited possible number of threads into the pool and it’s example usage. Now lets look example of fixed size thread pool executor which will help in improved performance and better system resource utilization by limiting the maximum number of …

Java Thread Pools and ThreadPoolExecutor

Why do we need a thread pool in Java? The answer is when we develop a simple, concurrent application in Java, we create some Runnable objects and then create the corresponding Thread objects to execute them. Creating a thread in Java is an expensive operation. And if you start creating …

ConcurrentLinkedDeque Example – Non-blocking Thread-safe List

In java, most used data structure is probably a list. A list has an undetermined number of elements and you can add, read, or remove the element of any position. Additionally, concurrent lists allow the various threads to add or remove elements in the list at a time without producing …

Java Binary Semaphore (+Example)

A binary semaphore is a synchronization primitive that can hold only two values, typically 0 and 1. We use binary semaphore to manage access to a shared resource by multiple threads in a concurrent environment. Conceptually, a binary semaphore can be thought of as a lock that allows only one …

Java Locks (with Examples)

Java provides another mechanism for the synchronization of blocks of code based on the Lock interface and classes that implement it (such as ReentrantLock). In this tutorial, we will see a basic usage of Lock interface to solve printer queue problem. 1. Lock Interface A java.util.concurrent.locks.Lock is a thread synchronization …

Creating Threads Using java.util.concurrent.ThreadFactory

The factory design pattern is one of the most used design patterns in the java. It is one of creational patterns and can be used to develop an object in demand of one or several classes. With this factory, we centralize the creation of objects. The centralization of creation logic …

Java ThreadLocal Example: When and How to Use?

Today, one of the most critical aspects of a concurrent application is shared data. When you create a thread that implements the Runnable interface and then starts various Thread objects using the same Runnable object. All the threads share the same attributes that are defined inside the runnable object. This …

Restarting threads using UncaughtExceptionHandler

1. UncaughtExceptionHandler Java applications have two kind of exceptions – checked exceptions and unchecked exceptions. Checked exceptions must be specified in the throws clause of a method or caught inside them. Unchecked exceptions don’t have to be specified or caught. When a checked exception is thrown inside the run() method …

Java Compare and Swap Example – CAS Algorithm

One of the best additions in java 5 was Atomic operations supported in classes such as AtomicInteger, AtomicLong etc. These classes help you in minimizing the need of complex (un-necessary) multi-threading code for some basic operations such as increment or decrement a value which is shared among multiple threads. These …

Java Concurrency – Difference between yield() and join()

Multi-threading is very popular topic among interviewers from long time. Though I personally feel that very few of us get real chance to work on a complex multi-threaded application (I got only one chance in last 7 years), still it helps in having the concepts handy to boost your confidence …

Java Concurrency – Thread Safety?

Defining thread safety is surprisingly tricky. A quick Google search turns up numerous “definitions” like these: Thread-safe code is code that will work even if many Threads are executing it simultaneously. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe …

Java CountDownLatch with Example

Learn the basics of CountDownLatch and how to use it in real-life applications, its methods and use it to control the flow of application.

Object level lock vs Class level lock in Java

In Java, a synchronized block of code can only be executed by one thread at a time. Also, java supports multiple threads to be executed concurrently. This may cause two or more threads to access the same fields or objects at same time. Synchronization is the process which keeps all …

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.