//-------------Wait Notify Example-------
package com.gs.corejava.datastructures;
class Studentt extends Thread {
public Studentt(Pen pen) {
System.out.println(String.format("%s Doing some work with pen ",
System.out.println(String.format("%s Going to wait for pen ",
} catch (InterruptedException e) {
System.out.println(String.format("%s Wait over again using pen",
public class WaitNotifyExample1 {
public static void main(String[] args) {
Thread st1 = new Thread(new Studentt(pen));
Thread st2 = new Thread(new Studentt(pen));
Thread-2 Doing some work with pen
Thread-0 Doing some work with pen
Thread-2 Going to wait for pen
Thread-0 Going to wait for pen
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.gs.corejava.datastructures.Studentt.run(WaitNotifyExample1.java:20)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.gs.corejava.datastructures.Studentt.run(WaitNotifyExample1.java:20)
at java.lang.Thread.run(Thread.java:619)
} catch (InterruptedException e) {
Thread-2 Doing some work with pen
Thread-0 Doing some work with pen
Thread-2 Going to wait for pen
Thread-0 Going to wait for pen
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.gs.corejava.datastructures.Studentt.run(WaitNotifyExample1.java:20)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.gs.corejava.datastructures.Studentt.run(WaitNotifyExample1.java:20)
at java.lang.Thread.run(Thread.java:619)
} catch (InterruptedException e) {
Thread-0 Doing some work with pen
Thread-2 Doing some work with pen
Thread-0 Going to wait for pen
Thread-2 Going to wait for pen
Thread-0 Wait over again using pen
Infinite Waiting for Thread-2 as no onw to wake up
package com.gs.corejava.datastructures;
class Studentt extends Thread {
public Studentt(Pen pen) {
System.out.println(String.format("%s Doing some work with pen ",
System.out.println(String.format("%s Going to wait for pen ",
} catch (InterruptedException e) {
System.out.println(String.format("%s Wait over again using pen",
public class WaitNotifyExample1 {
public static void main(String[] args) {
Thread st1 = new Thread(new Studentt(pen));
Thread st2 = new Thread(new Studentt(pen));
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.gs.corejava.datastructures.WaitNotifyExample1.main(WaitNotifyExample1.java:40)
Thread-2 Doing some work with pen
Thread-0 Doing some work with pen
Thread-2 Going to wait for pen
Thread-0 Going to wait for pen
Thread-2 Wait over again using pen
Infinite Waiting for Thread-0 as no onw to wake up
package com.gs.corejava.datastructures;
class Studentt extends Thread {
public Studentt(Pen pen) {
System.out.println(String.format("%s Doing some work with pen ",
System.out.println(String.format("%s Going to wait for pen ",
} catch (InterruptedException e) {
System.out.println(String.format("%s Wait over again using pen",
public class WaitNotifyExample1 {
public static void main(String[] args) {
Thread st1 = new Thread(new Studentt(pen));
Thread st2 = new Thread(new Studentt(pen));
Thread-2 Doing some work with pen
Thread-0 Doing some work with pen
Thread-2 Going to wait for pen
Thread-0 Going to wait for pen
Thread-2 Wait over again using pen
Infinite Waiting for Thread-0 as no onw to wake up
package com.gs.corejava.datastructures;
class Studentt extends Thread {
public Studentt(Pen pen) {
System.out.println(String.format("%s Doing some work with pen ",
System.out.println(String.format("%s Going to wait for pen ",
} catch (InterruptedException e) {
System.out.println(String.format("%s Wait over again using pen",
public class WaitNotifyExample1 {
public static void main(String[] args) {
Thread st1 = new Thread(new Studentt(pen));
Thread st2 = new Thread(new Studentt(pen));
} catch (InterruptedException e) {
Thread-0 Doing some work with pen
Thread-2 Doing some work with pen
Thread-0 Going to wait for pen
Thread-2 Going to wait for pen
Thread-0 Wait over again using pen
Thread-2 Wait over again using pen