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 synchronize the run() method as this method gets executed by a single thread only.

Continue

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 is mainly useful in multi-core processors where a condition becomes true quickly, i.e., in milliseconds or microseconds or even in nanoseconds.

Continue

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. An exchanger waits until both threads call its exchange() method to exchange data. An exchanger may be viewed as a bidirectional form of a Blocking Queue.

Continue

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 join was introduced to enhance the performance of the Java programming in which problems are solved by splitting them (recursively) into subtasks that are solved in parallel, waiting for them…

Continue

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

In this tutorial we 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 you need to ensure that t2 does not start until t1 finishes its execution, similarly t3 does not start until t2 finishes its execution. In this case we can write…

Continue

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 there is something in the shared object for your consumption, or in other word, another thread is waiting for something in the shared object. Then notify() or notifyAll() methods notify…

Continue

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 task is done, thread pool reuses the thread in the form of worker thread. The worker thread differently exists from Runnable or Callable tasks it executes and often used to…

Continue