Thursday, 7 April 2016

Print odd-even numbers using threads and wait-notify

This Java program prints odd-even numbers sequentially using two threads. One thread generates odd numbers and another even numbers.

This program makes use of inter-thread communication using wait, notify, notifyAll.

There is class SharedPrinter whose object is shared between two threads. In this class there is a method printEvenNum() for printing even numbers and method printOddNum() for printing odd numbers.

These two methods are called by the respective threads EvenNumProducer and OddNumProducer and these threads communicate using wait and notify of course from inside a synchronized block.

Java program

public class EvenOddThreadDemo {

 public static void main(String[] args) {
  // shared class object
  SharedPrinter sp = new SharedPrinter();
  // creating two threads
  Thread t1 = new Thread(new EvenNumProducer(sp, 10));
  Thread t2 = new Thread(new OddNumProducer(sp, 10));
  // starting threads
  t1.start();
  t2.start();
 }

}
// Shared class used by both threads
class SharedPrinter{
 boolean evenFlag = false;
 
 //Method for printing even numbers
 public void printEvenNum(int num){
  synchronized (this) {
    // While condition as mandated to avoid spurious wakeup
   while(!evenFlag){
    try {
     //asking current thread to give up lock
     wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   System.out.println(num);
   evenFlag = false;
   // Wake up thread waiting on this monitor(lock)
   notify();
   
  }
  
 }
 
 //Method for printing odd numbers
 public void printOddNum(int num){
  synchronized (this) {
    // While condition as mandated to avoid spurious wakeup
   while(evenFlag){
    try {
     //asking current thread to give up lock
     wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   System.out.println(num);
   evenFlag = true;
   // Wake up thread waiting on this monitor(lock)
   notify();
   
  }
 }
}

// Thread Class generating Even numbers
class EvenNumProducer implements Runnable{
    SharedPrinter sp;
    int index;
    EvenNumProducer(SharedPrinter sp, int index){
        this.sp = sp;
        this.index = index;
    }
    @Override
    public void run() {
        for(int i = 2; i <= index; i = i+2){
            sp.printEvenNum(i);
        }
        
    }
    
}

//Thread Class generating Odd numbers
class OddNumProducer implements Runnable{
    SharedPrinter sp;
    int index;
    OddNumProducer(SharedPrinter sp, int index){
        this.sp = sp;
        this.index = index;
    }
    @Override
    public void run() {
        for(int i = 1; i <= index; i = i+2){
            sp.printOddNum(i);
        }
    }
}

Output

1
2
3
4
5
6
7
8
9
10

That's all for this topic Printing odd even numbers using threads. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Inter-thread communication using wait, notify and notifyAll
  2. Why wait(), notify() and notifyAll() methods are in Object class
  3. Why wait(), notify() and notifyAll() must be called inside a synchronized method or block?
  4. Count total number of times each character appears in a String
  5. How to sort arraylist of custom objects in Java
  6. Count number of words in a String
  7. Print odd-even numbers using threads and semaphore

You may also like -

>>>Go to Java Programs page

3 comments:

  1. Valuable post with nice example. Thank you

    ReplyDelete
    Replies
    1. Thanks for the nice words... Glad I could be of some help!

      Delete