生产者消费者synchronized版实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class ProducerAndConsumer {
public static void main(String[] args) throws Exception {

Resources re = new Resources();

new Thread(() -> {
for (int i = 1;i <= 10;i++) {
try {
// 生产资源
re.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "T1").start();

new Thread(() -> {
for (int i = 1;i <= 10;i++) {
try {
// 消费资源
re.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "T2").start();
}
}

class Resources {

// 初始化资源
private int resource = 0;

public synchronized void produce() throws InterruptedException {

// 当还有资源的时候等待
while (resource != 0) {
this.wait();
}

// 生产资源的逻辑
resource++;

System.out.println(Thread.currentThread().getName() + ":" + resource);

// 通知
this.notifyAll();
}

public synchronized void consume() throws InterruptedException {

// 当没有资源的时候等待
while (resource == 0) {
this.wait();
}

// 消耗资源的逻辑
resource--;

System.out.println(Thread.currentThread().getName() + ":" + resource);

// 通知
this.notifyAll();
}
}

为什么使用 while 而不是使用 if ?

因为多线程中可能存在虚假唤醒问题,如果用 if 那当遇到虚假唤醒时直接停止 wait 进入到下一步程序中,但如果用 while 遇到虚假唤醒时程序会再次对 resource 进行判断资源条件是否满足以防止程序错误地进入下一步执行。

生产者消费者JUC版实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.concurrent.locks.Condition; 
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProducerAndConsumer {
public static void main(String[] args) throws Exception {

Resources re = new Resources();

new Thread(() -> {
for (int i = 1;i <= 10;i++) {
try {
// 生产资源
re.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "T1").start();

new Thread(() -> {
for (int i = 1;i <= 10;i++) {
try {
// 消费资源
re.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "T2").start();
}
}

class Resources {

// 初始化资源
private int resource = 0;

// 初始化锁
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();

public void produce() throws InterruptedException {

lock.lock();

try {

// 当还有资源的时候等待
while (resource != 0) {
condition.await();
}

// 生产资源的逻辑
resource++;

System.out.println(Thread.currentThread().getName() + ":" + resource);

// 通知
condition.signalAll();

} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}

public void consume() throws InterruptedException {

lock.lock();

try {

// 当还有资源的时候等待
while (resource == 0) {
condition.await();
}

// 生产资源的逻辑
resource--;

System.out.println(Thread.currentThread().getName() + ":" + resource);

// 通知
condition.signalAll();

} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}