Java Phaser Example

Phaser utility can be used in concurrent situations that are divided into multiple steps. Using it you can make sure that no threads will start the next phase until all threads have finished the previous one. At the basic level, the Phaser consists of two methods: .arriveAndAwaitAdvance() waits until all threads reach this method (a … Read more

Java CountDownLatch Example

Java CountDownLatch mechanism is similar to the semaphores. It is useful when you need to wait for a couple of threads to finish before continuing. How to use java’s CountDownLatch To use a CountDownLatch you need to instantiate it by giving the number (count) of operations as a paramter. Every time a operation finishes you … Read more

Java CyclicBarrier Example

CyclicBarrier is a part of Java’s java.util.concurrent.* package that offers various high level mechanisms for thread synchronization. A CyclicBarrier is especially useful when you need to wait for some threads to reach a common point. This is done using CyclicBarrier’s .await() method. Setup: You need multiple threads. You need a CyclicBarrier object in each thread/runnable. … Read more

Java Semaphores Example

Java semaphores implement a counter mechanism. This mechanism allows only a limited number of threads to enter a specified block of code. ava semaphore principles Semaphores are instantiated with a number of permits (number of threads that can concurrently access a resource). When a thread enters a block of code protected by semaphores it acquires … Read more

Java ReentrantLock Example

The java ReentrantLock is similar to the synchronized keyword mechanism. By using a Lock you allow only one thread to enter the code between .lock() and .unlock(). By using the same example from here, we can rewrite it using the java ReentrantLock object. The idea is the same. Let’s consider the BankAccount.java class: Main.java class: … Read more

Java wait, notify and notifyAll example

Java wait(), notify() and notifyAll() methods can be used for low level synchronization of threads. wait() forces the current thread to wait until another thread calls notify() or notifyAll() method of the object used by the thread. notifyAll() tells all currently waiting threads (that operate on the current object) that they can proceed notify() tells … Read more

Java synchronized keyword example

The java synchronized keyword allows only one thread to enter a method or a block of code. It is the quickest and most basic way of preventing data corruption caused by multiple threads running concurrently. Why use synchronized keyword? Multiple threads can access the same object or the same database connection at the same time. … Read more

Use interrupted exception to control thread

A thread can be stopped using interrupted exception. This is useful when your thread contains a recursive task or any kind of algorithm that consists of numerous methods. Interrupted exception example The idea Have the method(s) throw InterruptedException if thread is interrupted and in the run() method catch the exception and take the appropiate action. … Read more

Run vs Start method in java threads

Run vs start is an obvious question made by all people beginning java threads. What are the differences? Calling run() is like calling any ordinary method. It gets run on the current thread Calling start() creates a new execution thread that gets run concurrently with other existing threads Run vs start – code example Output: … Read more