-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProductTest.java
More file actions
132 lines (113 loc) · 3.26 KB
/
ProductTest.java
File metadata and controls
132 lines (113 loc) · 3.26 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Copyright (C)
* FileName: ProductTest
* Author: cp
* Date: 2019/7/28 21:28
* Description: 生产者消费者问题 基础写法
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package org.cp;
import java.util.LinkedList;
/**
* 〈一句话功能简述〉<br>
* 〈生产者消费者问题 基础写法〉
*
* @author cp
* @create 2019/7/28
* @since 1.0.0
*/
public class ProductTest {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer producer = new Producer(clerk);
producer.setName("生产者 1 ");
Consumer consumer = new Consumer(clerk);
consumer.setName("消费者 1 ");
Consumer consumer2 = new Consumer(clerk);
consumer2.setName("消费者 2 ");
producer.start();
consumer.start();
// consumer2.start();
}
}
/**
* 店员类 存放商品
*/
class Clerk{
//剩余商品数量
Integer productCount = 0;
Integer maxCount = 10;
private LinkedList<Object> list = new LinkedList<>();
public synchronized void produce() throws InterruptedException {
while (productCount>=maxCount) {//尽量用while来代替if
System.out.println(Thread.currentThread().getName()+"等待...");
wait();
}
productCount++;
System.out.println(Thread.currentThread().getName()+"生产第 "+productCount+" 个产品");
notifyAll();//尽量使用notifyAll()
}
public synchronized void consume() throws InterruptedException {
while (productCount<1) {
System.out.println(Thread.currentThread().getName()+"等待...");
wait();
}
System.out.println(Thread.currentThread().getName()+"消费第 "+productCount+" 个产品");
productCount--;
notifyAll();
}
}
/**
* 生产者
*
*/
class Producer extends Thread{
private Clerk clerk;
Producer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
while (true) {
//生产产品
try {
Long time;
synchronized (this) {
time = (Long.valueOf(clerk.maxCount)-Long.valueOf(clerk.productCount))/Long.valueOf(clerk.maxCount)*4000;
System.out.println(time);
}
Thread.sleep(time);
clerk.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费者
*/
class Consumer extends Thread {
private Clerk clerk;
Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
while (true) {
//消费产品
try {
Long time;
synchronized (this) {
time = Long.valueOf(clerk.productCount)/Long.valueOf(clerk.maxCount)*4000;
}
Thread.sleep(time);//sleep应该写在外面,synchronized里在允许的情况下,代码越少越好
clerk.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}