Skip to content

Commit 7a5c9a2

Browse files
committed
wait notify block
1 parent 2eb032e commit 7a5c9a2

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package CoreJava.advancedMultiThreading;
2+
3+
/**
4+
* Created by dheeraj on 7/18/2016.
5+
*/
6+
public class ThreadRunnableVsRun {
7+
8+
public static void main(String[] strings) {
9+
10+
Thread t = new Thread(new Runnable() {
11+
@Override
12+
public void run() {
13+
System.out.println("runnable run called");
14+
}
15+
}) {
16+
@Override
17+
public void run() {
18+
System.out.println("thread run called");
19+
}
20+
};
21+
t.start();
22+
}
23+
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package CoreJava.advancedMultiThreading;
2+
3+
/**
4+
* Created by dheeraj on 7/18/2016.
5+
*/
6+
public class WaitNotifyDemo {
7+
8+
public static void main(String[] strings) throws Exception {
9+
new Thread(new Runnable() {
10+
@Override
11+
public void run() {
12+
synchronized (WaitNotifyDemo.class) {
13+
try {
14+
WaitNotifyDemo.class.wait();
15+
} catch (Exception e) {
16+
}
17+
System.out.print("waiting done");
18+
}
19+
}
20+
}).start();
21+
Thread.sleep(1000);
22+
23+
24+
//above thread will only be notified after this syn is released after wait of 4 seconds
25+
synchronized (WaitNotifyDemo.class) {
26+
WaitNotifyDemo.class.notify();
27+
System.out.println("notified");
28+
Thread.sleep(4000);
29+
}
30+
}
31+
32+
33+
}

0 commit comments

Comments
 (0)