Print Odd And Even Numbers Using Two Threads in Java

In this post we’ll see how to print odd and even numbers using two threads in Java. This is an important Java multithreading interview question asked quite frequently.

Since two threads are used to alternatively print odd and even numbers so this program also shows how to synchronize threads and how inter thread communication works.

Printing odd and even numbers using two threads

You can use wait and notify methods to write this Java program or you can use Semaphore which is a synchronization aid in java.util.concurrent package.

Using wait() and notify() methods

In the Java program there are two Runnable tasks one for printing even numbers and another for printing odd numbers. Two threads are created to run these tasks and inter thread communication is done using wait notify.
There is also a class Printer with methods printEven() and printOdd(), instance of this class is shared between threads.

Output

Once the threads are started and execute run() method of their Runnable task printEven() and printOdd() methods are called. There based on the boolean flag one of the thread goes into wait state and other prints the number and also notifies the other thread using notify() method.

Using Semaphores

Semaphore implementation provided in Java is a counting Semaphore where Semaphore is initialized with permits. Critical section can only be executed after acquiring a permit and it can be released after the execution. Thread is blocked until a permit is available.

For printing odd and even numbers using two threads two Semaphores are used, one initialized with a single permit and another with zero permit. Semaphore with one permit is used for printing odd number and another Semaphore is used for printing even numbers, since it has zero permits initially that ensures even number is not printed first.

Output

Once the threads are started and execute run() method of their Runnable task printEven() and printOdd() methods are called. Since semaphoreOdd instance is initialized with one permit so it can acquire and run the code where as semaphoreEven instance has 0 permits so the other thread is blocked.
When semaphoreEven.release(); method is called from the printOdd() method that increments permit by 1 for semaphoreEven which can then be acquired in the printEven() method. Same way semaphoreOdd.release(); method is called in the printEven() method to release the already acquired permit of semaphoreOdd instance.

That’s all for the topic Print Odd And Even Numbers Using Two Threads in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • Race Condition in Java
  • Java CompletableFuture With Examples
  • Shallow Copy Vs Deep Copy in Java Object Cloning
  • Marker Interface in Java
  • Check if Given String Subsequence of Another String in Java
  • How to dual-boot Ubuntu and Windows
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.