Monday, March 20, 2017

java thread Wait & Notify example

WaitNotifyTest .java
------------------------------------------------------



package waitnotifytest;

public class WaitNotifyTest {

    public static void main(String[] args) {
        Message msg = new Message("process it");
        Waiter waiter = new Waiter(msg);
        new Thread(waiter,"waiter_1").start();
       
        Waiter waiter1 = new Waiter(msg);
        new Thread(waiter1, "waiter2").start();
       
        Notifier notifier = new Notifier(msg);
        new Thread(notifier, "notifier").start();
        System.out.println("All the threads are started");
    }

}







Waiter.java
----------------------------------------------------------------------

package waitnotifytest;

import java.util.Date;

public class Waiter implements Runnable{
   
    private Message msg;
   
    public Waiter(Message m){
        this.msg=m;
    }

    @Override
    public void run() {
        String thread_name = Thread.currentThread().getName();
       
        synchronized (msg) {
            try{
                System.out.println(thread_name+" waiting to get notified at time:"+System.currentTimeMillis()+ " min="+new Date().getMinutes()+" sec=" + new Date().getSeconds());
                msg.wait();
                System.out.println("--wait()");
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(thread_name+" after wait at time:"+System.currentTimeMillis()+ " min="+new Date().getMinutes()+" sec="  + new Date().getSeconds());
            //process the message now
            System.out.println(thread_name+" processed: "+msg.getMsg());
        }
    }

}








Notifier.java
------------------------------------------------------

package waitnotifytest;

public class Notifier implements Runnable {

    private Message msg;
   
    public Notifier(Message msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        String thread_name = Thread.currentThread().getName();
        System.out.println(thread_name+" started");
        try {
            Thread.sleep(1000);
            synchronized (msg) {
                msg.setMsg("Notifier thread_name=" + thread_name+" Notifier work done");
//                msg.notify();
                 msg.notifyAll();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       
    }

}


Message.java
-----------------------------------------------------------------------------------
package waitnotifytest;

public class Message {
    private String msg;
   
    public Message(String str){
        this.msg=str;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String str) {
        this.msg=str;
    }

}



###############output#################
All the threads are started
notifier started
waiter_1 waiting to get notified at time:1490077406336 min=23 sec=26
waiter2 waiting to get notified at time:1490077406342 min=23 sec=26
--wait()
waiter2 after wait at time:1490077407336 min=23 sec=27
waiter2 processed: Notifier thread_name=notifier Notifier work done
--wait()
waiter_1 after wait at time:1490077407336 min=23 sec=27
waiter_1 processed: Notifier thread_name=notifier Notifier work done

No comments:

Post a Comment