Home » Core Java » util » concurrent

concurrent

java.util.concurrent.atomic.AtomicLongArray Example

In this example we shall show you how to make use of AtomicLongArray class,  AtomicLongArray is a long array in which elements may be updated atomically. In a multithreading concurrent application architecture, we have a few tasks which are parallelized in such a way as to require no coordination between threads. The true problem comes when we need a coordination between ...

Read More »

java.util.concurrent.atomic.AtomicBoolean Example

In this example, we shall be demonstrating the use of java.util.concurrent.atomic.AtomicBoolean Class added in Java 5. The AtomicBoolean class is used to update a boolean value atomically i.e. only one thread can modify the boolean variable at a time. It provides two constructors, a parameterless one with default value as false and a constructor with argument as initial boolean value. ...

Read More »

Java.util.concurrent.CyclicBarrier Example

In one of the previous posts, we talked about CountdownLatch and its use case. Today we are going to talk about a similar yet different synchronisation mechanism called as CyclicBarrier. Java.util.Concurrent.CyclicBarrier is a synchronisation aid that allows a set of threads to wait for each other to reach a common barrier point. This means that all the threads reaching a ...

Read More »

java.util.concurrent.RecursiveTask Example

In this example we shall show you how to make use of Java RecursiveTask class, RecursiveTask provides a convenient way to consolidate results from subtasks. Java 7 introduced another implementation of ExecutorService: the ForkJoinPool class. ForkJoinPool is designed for handling tasks efficiently that can be repeatedly broken down into subtasks, using the RecursiveAction class (when the task produces no result) or ...

Read More »

Java AtomicMarkableReference Example

In this example we shall make use of AtomicMarkableReference class of Java. It is another class under the java.util.concurrent.atomic package, which provides lock-free and thread-safe programming solutions. In a recent example, we have seen how the AtomicReference class can be utilized to provide reference objects that may be atomically updated. Atomic actions are actions performed in an object only by ...

Read More »

java.util.concurrent.Exchanger Example

In this example, we shall be demonstrating how we can use the java.util.concurrent.Exchanger Class in Java. The Exchanger Class provides a sort of rendezvous point for two threads, where the threads can exchange their respective Objects with the other thread. Whenever a thread arrives at the exchange point, it must wait for the other thread to arrive. When the other ...

Read More »

java.util.concurrent.ThreadLocalRandom Example

In this example we shall show you how to make use of ThreadLocalRandom class, ThreadLocalRandom is a random number generator like its parent Random class. Like the global Random generator class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified.         1. ThreadLocalRandom Advantages ThreadLocalRandom provide more good performance and less overhead in ...

Read More »

java.util.concurrent.ScheduledThreadPoolExecutor Example

In this example, we shall be demonstrating how we can use the java.util.concurrent.ScheduledThreadPoolExecutor Class to schedule some periodic Tasks at fixed rate or fixed delay. 1. Timer Timer task is used also used to schedule activities. However, the Timer object can run only Task at a time. As a result of which, if the previous task is taking a more ...

Read More »

java.util.concurrent.FutureTask Example

In this example we shall show you how to make FutureTask, FutureTask is an implementation of Future which offers a very elegant way to implement parallel execution of tasks in Java where a big task can be split into small chunks and if each of those chunks can be executed in parallel, it can result in better response times and throughput. We can ...

Read More »

java.util.concurrent.Phaser Example

In this example, we shall be demonstrating the java.util.concurrent.Phaser Class. The Phaser Class was introduced in Java 7. Phaser is like a collection of advantages of CountDownLatch and CyclicBarrier Classes.             CountDownLatch vs CyclicBarrier vs Phaser The CountDownLatch is : Created with a fixed number of threads Cannot be reset Allows threads to wait(CountDownLatch#await()) or ...

Read More »