Completable Future To Fetch Data Asynchronously

Asynchronous Data Here I am going to show you how to fetch data asynchronously. So, data which are not coordinated in time will be fetched any time. This means that data is sent at irregular intervals, without any specific time or synchronization between the sender and receiver. In this example I am going to use CompletableFuture from Java 8’s new…

Difference Between Parallel Stream And CompletableFuture In Java

Parallel Stream Vs CompletableFuture 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, whereas, 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…

Should we synchronize run() method?

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 it is not necessary to…

Busy Waiting or Spinning Example in Java Multi-threading

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 CPU. Busy waiting or spinning…

How to exchange data between two threads using Exchanger in Java

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() method of the Exchanger class….

How Fork Join works 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 was one of them. Fork…

How Deadlock Occurs And How To Fix It In Java

Deadlock 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 applications.

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

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 started execution in order and…

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

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 to tell another thread that…

Writing Junit Test on Java Thread

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.