Skip to content

Commit e4d3872

Browse files
committed
🔖 Thread 示例
1 parent f4ebab3 commit e4d3872

3 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.github.dunwu.javase.concurrent.thread;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
/**
9+
* 利用 Condition 实现生产者-消费者模型
10+
* @author Zhang Peng
11+
* @date 2018/5/11
12+
* @see ThreadWaitNotifyDemo02
13+
*/
14+
public class ConditionDemo {
15+
16+
private int queueSize = 10;
17+
private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);
18+
private Lock lock = new ReentrantLock();
19+
private Condition notFull = lock.newCondition();
20+
private Condition notEmpty = lock.newCondition();
21+
22+
public static void main(String[] args) {
23+
ConditionDemo demo = new ConditionDemo();
24+
Producer producer = demo.new Producer();
25+
Consumer consumer = demo.new Consumer();
26+
27+
producer.start();
28+
consumer.start();
29+
}
30+
31+
class Consumer extends Thread {
32+
33+
@Override
34+
public void run() {
35+
consume();
36+
}
37+
38+
private void consume() {
39+
while (true) {
40+
lock.lock();
41+
try {
42+
while (queue.size() == 0) {
43+
try {
44+
System.out.println("队列空,等待数据");
45+
notEmpty.await();
46+
} catch (InterruptedException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
queue.poll(); //每次移走队首元素
51+
notFull.signal();
52+
System.out.println("从队列取走一个元素,队列剩余" + queue.size() + "个元素");
53+
} finally {
54+
lock.unlock();
55+
}
56+
}
57+
}
58+
}
59+
60+
class Producer extends Thread {
61+
62+
@Override
63+
public void run() {
64+
produce();
65+
}
66+
67+
private void produce() {
68+
while (true) {
69+
lock.lock();
70+
try {
71+
while (queue.size() == queueSize) {
72+
try {
73+
System.out.println("队列满,等待有空余空间");
74+
notFull.await();
75+
} catch (InterruptedException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
queue.offer(1); //每次插入一个元素
80+
notEmpty.signal();
81+
System.out.println("向队列取中插入一个元素,队列剩余空间:" + (queueSize - queue.size()));
82+
} finally {
83+
lock.unlock();
84+
}
85+
}
86+
}
87+
}
88+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.dunwu.javase.concurrent.thread;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author Zhang Peng
7+
* @date 2018/5/11
8+
*/
9+
public class ThreadWaitNotifyDemo {
10+
11+
public static void main(String[] args) {
12+
final Object lock = new Object();
13+
14+
new Thread(() -> {
15+
System.out.println("thread A is waiting to get lock");
16+
synchronized (lock) {
17+
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");
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
}).start();
28+
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();
37+
}
38+
lock.notify();
39+
System.out.println("thread B do notify method");
40+
}
41+
}).start();
42+
}
43+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.dunwu.javase.concurrent.thread;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* 利用 wait() 、notify() 实现生产者-消费者模型
7+
* @author Zhang Peng
8+
* @date 2018/5/11
9+
* @see ConditionDemo
10+
*/
11+
public class ThreadWaitNotifyDemo02 {
12+
13+
private int queueSize = 10;
14+
private final PriorityQueue<Integer> queue = new PriorityQueue<>(queueSize);
15+
16+
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();
23+
}
24+
25+
class Consumer extends Thread {
26+
27+
@Override
28+
public void run() {
29+
consume();
30+
}
31+
32+
private void consume() {
33+
while (true) {
34+
synchronized (queue) {
35+
while (queue.size() == 0) {
36+
try {
37+
System.out.println("队列空,等待数据");
38+
queue.wait();
39+
} catch (InterruptedException e) {
40+
e.printStackTrace();
41+
queue.notify();
42+
}
43+
}
44+
queue.poll(); // 每次移走队首元素
45+
queue.notify();
46+
System.out.println("从队列取走一个元素,队列剩余" + queue.size() + "个元素");
47+
}
48+
}
49+
}
50+
}
51+
52+
class Producer extends Thread {
53+
54+
@Override
55+
public void run() {
56+
produce();
57+
}
58+
59+
private void produce() {
60+
while (true) {
61+
synchronized (queue) {
62+
while (queue.size() == queueSize) {
63+
try {
64+
System.out.println("队列满,等待有空余空间");
65+
queue.wait();
66+
} catch (InterruptedException e) {
67+
e.printStackTrace();
68+
queue.notify();
69+
}
70+
}
71+
queue.offer(1); // 每次插入一个元素
72+
queue.notify();
73+
System.out.println("向队列取中插入一个元素,队列剩余空间:" + (queueSize - queue.size()));
74+
}
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)