Java Concurrency: Count Down Latches

Hello, readers, and welcome to yet another blog in the Java Concurrency series. Today, we are going to look at the CountDownLatch class in Java, what it is, and how to use it. So, let's dive straight into it.

CountDownLatch in Java

Sometimes, we have a need to start our application only when a particular set of tasks are complete. These tasks might be running in parallel and getting completed together or at different times. Then, how do we tell our other threads that all the tasks are completed? How do we keep track of which tasks are complete and which are not? CountDownLatch is a class just for that.

Java Concurrency: Thread Confinement

Hello, readers! In this blog, we are going to explore thread confinement, what it means, and how we achieve it. So, let’s dive straight into it.

Thread Confinement

Most concurrency problems occur only when we want to share a mutable variable, or mutable state, between threads. If a mutable state is shared between multiple threads, then all of them will be able to read and modify the value of the state, thus resulting in incorrect or unexpected behavior. One way to avoid this problem is to simply not share the data between the threads. This technique is known as thread confinement and is one of the simplest ways of achieving thread safety in our application.