Home » Core Java » util » concurrent

concurrent

Java AtomicReference Example

This is an example of the AtomicReference class of Java. It is another class under the java.util.concurrent.atomic package, which provides an object reference that may be updated atomically. The AtomicReference class provides reference objects that may be read and written atomically, so when multiple threads try to reach them at the same time, only one will be able to do ...

Read More »

java.util.concurrent.ForkJoinPool Example

In this example, we shall be demonstrating the use of java.util.concurrent.ForkJoinPool Class. This class was introduced in Java 7. The java.util.concurrent.ForkJoinPool Class implements java.util.concurrent.Executor and java.util.concurrent.ExecutorService interfaces. This class works on divide-and-conquer policy. Each major subtask is divided into a number of sub-tasks and a new thread is spawned for each new sub-task. Recursion is the most popular strategy for ...

Read More »

Java AtomicIntegerArray Example

In this example we shall talk about the AtomicIntegerArray class of Java. Java provides the java.util.concurrent.atomic package specification for lock-free and thread-safe programming on single variables. The AtomicIntegerArray class is an int array in which elements may be updated atomically. Its difference with an simple AtomicInteger[] is that it creates one object, whereas when using AtomicInteger[], one object per array ...

Read More »

java.util.concurrent.Semaphore Example

In this example we shall show you how to make use of use of java.util.concurrent.Semaphore Class in Java. 1. What is a SEMAPHORE? A semaphore in Computer Science is analogous to a Guard at the entrance of a building. However, this guard takes into account the number of people already entered into the building. At any given time, there can ...

Read More »

java.util.concurrent.CountDownLatch Example

In this example we will see how and when to use java.util.concurrent.CountDownLatch CountDownLatch is used in synchronisation to allow one or more threads to wait until a set of operations being performed in other threads completes. For Example, it can be used when a Thread has to wait until few dependent threads have started. CountDownLatch is initialised with a given count. This ...

Read More »

Java AtomicInteger Example

This is an example of how to use the AtomicInteger class of Java. The java.util.concurrent.atomic package provides very useful classes that support lock-free and thread-safe programming on single variables. Among them, the AtomicInteger class is a wrapper class for an int value that allows it to be updated atomically. The class provides useful methods, some of which will be shown ...

Read More »

Java ReadWriteLock Example

In this example, we are going to demonstrate the use of ReadWriteLock in Java. Mutually Exclusive Locks, as the ReentrantLock discussed in the previous article, offer far less level of concurrency than non-mutually exclusive locks. If multiple threads are accessing an object for reading data, it does not make sense to use a synchronized block or any other mutually exclusive ...

Read More »

java.util.concurrent.ForkJoinWorkerThread Example

In this post, we are going to discuss about the class java.util.concurrent.ForkJoinWorkerThread and give you and idea of how you can use it on your own code when building robust multi-threaded applications. 1. ForkJoinWorkerThread Class One of the most interesting features of Java 7 is the Fork/Join framework. It’s an implementation of the Executor and ExecutorService interfaces that allow you ...

Read More »

java.util.concurrent.locks.Condition Example

In this tutorial we will discuss about the Condition interface in Java. A Condition object, also known as condition variable, provides a thread with the ability to suspend its execution, until the condition is true. A Condition object is necessarily bound to a Lock and can be obtained using the newCondition() method. Furthermore, a Condition enables the effect of having ...

Read More »

Java ReentrantLock Example

In this example, we shall be demonstrating how to use ReentrantLock in Java. Rigidness of Intrinsic Locking : The traditional way of providing synchronization in multi-threaded environment was to use the synchronized keyword. However, the synchronized keyword is considered rather rigid under certain circumstances.For example, if a thread is already executing inside a synchronized block and another thread tries to enter the ...

Read More »