Posted in Java

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…

Continue Reading... Difference Between Parallel Stream And CompletableFuture In Java
Posted in Java

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…

Continue Reading... Should we synchronize run() method?
Posted in Java

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…

Continue Reading... Busy Waiting or Spinning Example in Java Multi-threading
Posted in Java

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….

Continue Reading... How to exchange data between two threads using Exchanger in Java
Posted in Java

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…

Continue Reading... How Fork Join works in Java
Posted in Java

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.

Continue Reading... How Deadlock Occurs And How To Fix It In Java
Posted in Java

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…

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

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…

Continue Reading... Inter Thread Communication using wait(), notify() and notifyAll() in Java
Posted in Java Junit

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.

Continue Reading... Writing Junit Test on Java Thread
Posted in Java

How to create Custom Thread Pool in Java

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 and discarding thread once the…

Continue Reading... How to create Custom Thread Pool in Java