Java Threads Print Alternative Odd Even Numbers

Java Threads Print Alternative Odd Even Numbers

This article shows how to print Even and Odd numbers alternatively using Java. It uses Two Java threads and a Class with two methods to print even numbers and odd numbers respectively.

What we are trying to achieve

We want to print even and odd numbers alternatively but using two threads. Essentially it is the interaction between two threads utilizing the wait() and notify() methods. One thread completes a task (printing a single even number) then it is put on wait state so that the other thread can get a chance to do its job (printing odd number) and this goes on until we are done with printing a set of numbers determined by max.
How it will work
  • Print Class (Print.java) with two synchronized methods even() and odd() which print even or odd numbers respectively.
  • Each method is synchronized so that two threads cannot execute them concurrently.
  • Each method first calls notifyAll() to so that other threads that are waiting can be wake up.
  • It then prints a number (even or odd) and then calls the wait() method on the current thread so it goes to waiting state.

Java Code

Print.java
This Class essentially has two synchronized methods. The constructor of this class takes a max number as a parameter. This max number is the limit to which the numbers will be printed. It has two synchronized methods to print even and odd numbers alternatively.
class Print {
  int max;

  public Print(int max) {
    this.max = max;
  }

  public synchronized void even() throws InterruptedException {
    for (int i = 0; i <= max; i++) {
      notifyAll();
      if (i % 2 == 0)
        System.out.println(Thread.currentThread().getName() + ":: " + i);
      wait();
    }
  }

  public synchronized void odd() throws InterruptedException {
    for (int i = 0; i <= max; i++) {
      notifyAll();
      if (i % 2 != 0)
        System.out.println(Thread.currentThread().getName() + ":: " + i);
      wait();
    }
  }
}
Threads.java
Here we are creating an instance of Print class and creating two Threads. From the first thread, we are calling print.even() method, from the second thread we are calling print.odd() method.
Print print = new Print(10);

//Thread to print even numbers
Thread t1 = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      print.even();
    } catch (InterruptedException e) {
    }
  }
});

//Thread to print odd numbers
Thread t2 = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      print.odd();
    } catch (InterruptedException e) {
    }
  }
});

t1.setName("Even Thread");
t2.setName(" Odd Thread");

t1.start();
t2.start();
Console Output
Even Thread:: 0
Odd Thread:: 1
Even Thread:: 2
Odd Thread:: 3
Even Thread:: 4
Odd Thread:: 5
Even Thread:: 6
Odd Thread:: 7
Even Thread:: 8
Odd Thread:: 9
Even Thread:: 10
Conclusion
  • From the odd() or even () method we have to first call notify or notifyAll() first.
  • Both odd() or even() method should be synchronized otherwise calling notifyAll() or wait() will throw exception

No comments :

Post a Comment

Please leave your message queries or suggetions.