Skip to content

Commit 177373b

Browse files
committed
🔖 线程示例
1 parent b96d386 commit 177373b

3 files changed

Lines changed: 107 additions & 54 deletions

File tree

‎codes/concurrent/src/main/java/io/github/dunwu/javase/concurrent/thread/ThreadStopDemo02.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
public class ThreadStopDemo02 {
88

99
public static void main(String[] args) {
10-
ThreadStop my = new ThreadStop();
11-
Thread t = new Thread(my, "线程"); // 建立线程对象
12-
t.start(); // 启动线程
10+
MyThread my = new MyThread();
11+
new Thread(my, "线程").start();
1312
try {
14-
Thread.sleep(30);
15-
} catch (Exception e) {
13+
Thread.sleep(1000);
14+
} catch (InterruptedException e) {
1615
e.printStackTrace();
1716
}
1817
my.stop(); // 修改标志位,停止运行
1918
}
2019

21-
static class ThreadStop implements Runnable {
20+
static class MyThread implements Runnable {
2221

2322
private volatile boolean flag = true; // 定义标志位
2423

@@ -28,6 +27,7 @@ public void run() {
2827
while (this.flag) {
2928
System.out.println(Thread.currentThread().getName() + "运行,i = " + (i++));
3029
}
30+
System.out.println(Thread.currentThread().getName() + "线程停止");
3131
}
3232

3333
void stop() {
Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,88 @@
11
package io.github.dunwu.javase.concurrent.thread;
22

3-
import java.util.concurrent.TimeUnit;
4-
53
/**
64
* @author Zhang Peng
75
* @date 2018/5/11
86
*/
97
public class ThreadWaitNotifyDemo {
108

119
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+
}
1338

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) {
1751
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();
2354
} catch (InterruptedException e) {
2455
e.printStackTrace();
2556
}
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());
2660
}
27-
}).start();
61+
}
62+
}
2863

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();
3782
}
38-
lock.notify();
39-
System.out.println("thread B do notify method");
83+
} catch (InterruptedException e) {
84+
e.printStackTrace();
4085
}
41-
}).start();
86+
}
4287
}
4388
}

‎codes/concurrent/src/main/java/io/github/dunwu/javase/concurrent/thread/ThreadWaitNotifyDemo02.java‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@
1010
*/
1111
public class ThreadWaitNotifyDemo02 {
1212

13-
private int queueSize = 10;
14-
private final PriorityQueue<Integer> queue = new PriorityQueue<>(queueSize);
13+
private static final int QUEUE_SIZE = 10;
14+
private static final PriorityQueue<Integer> queue = new PriorityQueue<>(QUEUE_SIZE);
1515

1616
public static void main(String[] args) {
17-
ThreadWaitNotifyDemo02 test = new ThreadWaitNotifyDemo02();
18-
Producer producer = test.new Producer();
19-
Consumer consumer = test.new Consumer();
20-
21-
producer.start();
22-
consumer.start();
17+
new Producer("生产者A").start();
18+
new Producer("生产者B").start();
19+
new Consumer("消费者A").start();
20+
new Consumer("消费者B").start();
2321
}
2422

25-
class Consumer extends Thread {
23+
static class Consumer extends Thread {
2624

27-
@Override
28-
public void run() {
29-
consume();
25+
Consumer(String name) {
26+
super(name);
3027
}
3128

32-
private void consume() {
29+
@Override
30+
public void run() {
3331
while (true) {
3432
synchronized (queue) {
3533
while (queue.size() == 0) {
@@ -38,39 +36,49 @@ private void consume() {
3836
queue.wait();
3937
} catch (InterruptedException e) {
4038
e.printStackTrace();
41-
queue.notify();
39+
queue.notifyAll();
4240
}
4341
}
4442
queue.poll(); // 每次移走队首元素
45-
queue.notify();
46-
System.out.println("从队列取走一个元素,队列剩余" + queue.size() + "个元素");
43+
queue.notifyAll();
44+
try {
45+
Thread.sleep(500);
46+
} catch (InterruptedException e) {
47+
e.printStackTrace();
48+
}
49+
System.out.println(Thread.currentThread().getName() + " 从队列取走一个元素,队列当前有:" + queue.size() + "个元素");
4750
}
4851
}
4952
}
5053
}
5154

52-
class Producer extends Thread {
55+
static class Producer extends Thread {
5356

54-
@Override
55-
public void run() {
56-
produce();
57+
Producer(String name) {
58+
super(name);
5759
}
5860

59-
private void produce() {
61+
@Override
62+
public void run() {
6063
while (true) {
6164
synchronized (queue) {
62-
while (queue.size() == queueSize) {
65+
while (queue.size() == QUEUE_SIZE) {
6366
try {
6467
System.out.println("队列满,等待有空余空间");
6568
queue.wait();
6669
} catch (InterruptedException e) {
6770
e.printStackTrace();
68-
queue.notify();
71+
queue.notifyAll();
6972
}
7073
}
7174
queue.offer(1); // 每次插入一个元素
72-
queue.notify();
73-
System.out.println("向队列取中插入一个元素,队列剩余空间:" + (queueSize - queue.size()));
75+
queue.notifyAll();
76+
try {
77+
Thread.sleep(500);
78+
} catch (InterruptedException e) {
79+
e.printStackTrace();
80+
}
81+
System.out.println(Thread.currentThread().getName() + " 向队列取中插入一个元素,队列当前有:" + queue.size() + "个元素");
7482
}
7583
}
7684
}

0 commit comments

Comments
 (0)