I am going to discuss here CompletableFuture vs Parallel Stream in Java programming language. CompletableFuture extends Future with added advantage to allow the tasks finish in an ad hoc manner. In Parallel Stream task is divided into sub-tasks and run on separate threads to be completed faster. Both CompletableFuture and Parallel Stream were added in…

Posted in Java

Difference Between Parallel Stream and CompletableFuture in Java

Here in this tutorial we will discuss whether we should synchronize the run() method or not. Another question may arise can we synchronize run() method? In simple word, yes, we can synchronize the run() method. Do we need to synchronize the run method? When it comes to whether we need to synchronize or not, then…

Posted in Java

Should we synchronize run() method?

What is busy waiting or spinning? Busy spinning or waiting in multi-threading environment is a technique in which a process repeatedly checks if a particular condition is true instead of wait() or sleep() method and without releasing the CPU. In other words busy spinning is one of the techniques to wait for events without releasing…

Posted in Java

Busy Waiting or Spinning Example in Java Multi-threading

We will discuss about how to exchange data between two threads or a pair of threads. The Exchanger class under java.util.concurrent package was introduced in JDK 1.5 along with CyclicBarrier, CountDownLatch to exchange data between two threads only. A synchronization point at which two threads can pair and exchange data within pair using the exchange()…

Posted in Java

How to exchange data between two threads using Exchanger in Java

Introduction Here I am going to tell you how fork join works in Java and how to use fork join in Java. Fork join was introduced in java version 1.7. JDK 7 and Java version 7 introduced many good features, such as, string in switch case, multi-catch block, automatic resource management, etc. and fork join…

Posted in Java

How Fork Join works in Java

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Java deadlock situation arises with at least two threads(multitasking or multi-threading) and two or more resources. There are several ways you can detect deadlock in Java application.

Posted in Java

How Deadlock occurs and how to fix it in Java

In this tutorial I will discuss about the join() method from Thread class. This method is important method from Thread class and it imposes order on execution of the threads. Therefore join() method ensures that multiple threads run in sequence, i.e., in the same order the threads were started. Let’s say threads t1, t2, t3…

Posted in Java

How to execute Java Threads in the same Order they were started using join() Method

We will discuss here what are the purpose of wait(), notify() and notifyAll() methods in Java. These methods are used in inter-thread communication. Each thread in Java has its own separate path of execution, so when you need to establish communication among threads then you need to use these methods. Let’s say a thread wants…

Posted in Java

Inter Thread Communication using wait(), notify() and notifyAll() in Java

Introduction In this tutorial you will see how to write test case on Java thread using junit. Writing junit test on Java thread will show an example on single threaded environment.

Posted in Java Junit

Writing Junit Test on Java Thread

Introduction In this tutorial I am going to show how to create custom thread pool in Java. Multi-threaded and multi-process programmings are great ways to optimize CPU usage and get things done quickly. What is thread pool? Thread pool is a collection of already created worker threads ready to perform certain tasks. Instead of creating…

Posted in Java

How to create Custom Thread Pool in Java

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are…

Posted in Java

Java CyclicBarrier

What is CountDownLatch? A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes, i.e., a kind of synchronization tool that allows one Thread  to wait for one or more Threads before it starts processing. How does CountDownLatch work? A CountDownLatch is initialized with…

Posted in Java

Java CountDownLatch