Thursday, September 23, 2010

Example for Understanding Java's Wait / Notify Mechanism and Synchronization

 public class WaitNotifyExample {

    public static void main (String[] args) {
        final Object obj = new Object();

        Runnable r = new Runnable () {
            public void run () {
                while (true) {
                    try {
                        System.out.println ("-- this lines are being printed in another thread.." + Thread.currentThread ().getName ());
                        synchronized(obj){// The current thread must own this object's monitor see javadoc guys..Object.wait ();
                            obj.wait();// just wait for somebody else to awake you here.
                        }
                    } catch (Exception ex) {
                        System.out.println (" -- Interrupted...");
                        ex.printStackTrace ();
                    }
                }
            }
        };

        new Thread (r).start ();//we crated and started a lazy guy here
        new Thread (r).start ();//another lazy guy
        new Thread (r).start ();//this can be me :)

        while (true) {
            System.out.println (" -- this lines are bing printed in main thread.." + Thread.currentThread ().getName ());
            try {

                Thread.currentThread ().sleep (5000);//Our master will sleep for 5 seconds
                synchronized(obj){//same reason with above synchronized
                    obj.notifyAll();// Then it will say others wake up !
                }
                Thread.currentThread ().sleep (500);//If we don'T stop the main thread here it will be the owner of the monitor of obj object always.
                                                    //It is in a while loop.Others will be blocked on synchronized statement.

            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}



---| Lets look at to the system out , I have took a thread dump for u also  |-----
-- this lines are being printed in another thread..Thread-0
-- this lines are being printed in another thread..Thread-1
 -- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-2
-- this lines are being printed in another thread..Thread-0
-- this lines are being printed in another thread..Thread-1
 -- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-2
-- this lines are being printed in another thread..Thread-0
-- this lines are being printed in another thread..Thread-1
Full thread dump Java HotSpot(TM) Server VM (1.5.0_08-b03 mixed mode):

"Thread-2" prio=1 tid=0x09dcaa88 nid=0xfa6 in Object.wait() [0x6e8cb000..0x6e8cc080]
    at java.lang.Object.wait(Native Method)
    - waiting on <0xaa00e258> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:474)
    at sil.WaitNotifyExample$1.run(WaitNotifyExample.java:17)//
obj.wait()
    - locked <0xaa00e258> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:595)

"Thread-1" prio=1 tid=0x09dc9218 nid=0xfa5 in Object.wait() [0x6e94c000..0x6e94d100]
    at java.lang.Object.wait(Native Method)
    - waiting on <0xaa00e258> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:474)
    at sil.WaitNotifyExample$1.run(WaitNotifyExample.java:17)//
obj.wait()
    - locked <0xaa00e258> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:595)

"Thread-0" prio=1 tid=0x09dcb550 nid=0xfa4 in Object.wait() [0x6e9cd000..0x6e9cdd80]
    at java.lang.Object.wait(Native Method)
    - waiting on <0xaa00e258> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:474)
    at sil.WaitNotifyExample$1.run(WaitNotifyExample.java:17)//
obj.wait()
    - locked <0xaa00e258> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:595)

"main" prio=1 tid=0x09cecee0 nid=0xf94 waiting on condition [0xbfe77000..0xbfe776f8]
    at java.lang.Thread.sleep(Native Method)
    at sil.WaitNotifyExample.main(WaitNotifyExample.java:37)//
Thread.currentThread ().sleep (500)

No comments:

Post a Comment