|
1 | 1 | package io.github.dunwu.javase.concurrent.thread; |
2 | 2 |
|
3 | | -import java.util.concurrent.TimeUnit; |
4 | | - |
5 | 3 | /** |
6 | 4 | * @author Zhang Peng |
7 | 5 | * @date 2018/5/11 |
8 | 6 | */ |
9 | 7 | public class ThreadWaitNotifyDemo { |
10 | 8 |
|
11 | 9 | public static void main(String[] args) { |
12 | | - final Object lock = new Object(); |
| 10 | + Message msg = new Message("process it"); |
| 11 | + Waiter waiter = new Waiter(msg); |
| 12 | + new Thread(waiter, "waiter").start(); |
| 13 | + |
| 14 | + Waiter waiter1 = new Waiter(msg); |
| 15 | + new Thread(waiter1, "waiter1").start(); |
| 16 | + |
| 17 | + Notifier notifier = new Notifier(msg); |
| 18 | + new Thread(notifier, "notifier").start(); |
| 19 | + System.out.println("All the threads are started"); |
| 20 | + } |
| 21 | + |
| 22 | + static class Message { |
| 23 | + |
| 24 | + private String msg; |
| 25 | + |
| 26 | + Message(String str) { |
| 27 | + this.msg = str; |
| 28 | + } |
| 29 | + |
| 30 | + String getMsg() { |
| 31 | + return msg; |
| 32 | + } |
| 33 | + |
| 34 | + void setMsg(String str) { |
| 35 | + this.msg = str; |
| 36 | + } |
| 37 | + } |
13 | 38 |
|
14 | | - new Thread(() -> { |
15 | | - System.out.println("thread A is waiting to get lock"); |
16 | | - synchronized (lock) { |
| 39 | + static class Waiter implements Runnable { |
| 40 | + |
| 41 | + private Message msg; |
| 42 | + |
| 43 | + Waiter(Message m) { |
| 44 | + this.msg = m; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void run() { |
| 49 | + String name = Thread.currentThread().getName(); |
| 50 | + synchronized (msg) { |
17 | 51 | try { |
18 | | - System.out.println("thread A get lock"); |
19 | | - TimeUnit.SECONDS.sleep(1); |
20 | | - System.out.println("thread A do wait method"); |
21 | | - lock.wait(); |
22 | | - System.out.println("wait end"); |
| 52 | + System.out.println(name + " waiting to get notified at time:" + System.currentTimeMillis()); |
| 53 | + msg.wait(); |
23 | 54 | } catch (InterruptedException e) { |
24 | 55 | e.printStackTrace(); |
25 | 56 | } |
| 57 | + System.out.println(name + " waiter thread got notified at time:" + System.currentTimeMillis()); |
| 58 | + //process the message now |
| 59 | + System.out.println(name + " processed: " + msg.getMsg()); |
26 | 60 | } |
27 | | - }).start(); |
| 61 | + } |
| 62 | + } |
28 | 63 |
|
29 | | - new Thread(() -> { |
30 | | - System.out.println("thread B is waiting to get lock"); |
31 | | - synchronized (lock) { |
32 | | - System.out.println("thread B get lock"); |
33 | | - try { |
34 | | - TimeUnit.SECONDS.sleep(5); |
35 | | - } catch (InterruptedException e) { |
36 | | - e.printStackTrace(); |
| 64 | + static class Notifier implements Runnable { |
| 65 | + |
| 66 | + private Message msg; |
| 67 | + |
| 68 | + Notifier(Message msg) { |
| 69 | + this.msg = msg; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void run() { |
| 74 | + String name = Thread.currentThread().getName(); |
| 75 | + System.out.println(name + " started"); |
| 76 | + try { |
| 77 | + Thread.sleep(1000); |
| 78 | + synchronized (msg) { |
| 79 | + msg.setMsg(name + " Notifier work done"); |
| 80 | + msg.notify(); |
| 81 | + // msg.notifyAll(); |
37 | 82 | } |
38 | | - lock.notify(); |
39 | | - System.out.println("thread B do notify method"); |
| 83 | + } catch (InterruptedException e) { |
| 84 | + e.printStackTrace(); |
40 | 85 | } |
41 | | - }).start(); |
| 86 | + } |
42 | 87 | } |
43 | 88 | } |
0 commit comments