-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProdcuceCosumer3.java
More file actions
54 lines (46 loc) · 1.12 KB
/
ProdcuceCosumer3.java
File metadata and controls
54 lines (46 loc) · 1.12 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
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* BlockingQueue<Object>
*/
public class ProdcuceCosumer3 {
private static final int MAX = 100;
private static BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(MAX);
public static void main(String[] args) {
new Producer().start();
new Comsumer().start();
}
static class Producer extends Thread{
@Override
public void run() {
while(true) {
if (queue.size() == MAX){
System.out.println("queue full");
}
try {
queue.put(new Object());
System.out.println("put one , the queue size is : " + queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Comsumer extends Thread{
@Override
public void run() {
while(true) {
if (queue.size() == 0){
System.out.println("queue empty");
}
try {
queue.take();
System.out.println("get one , the queue size is : " + queue.size());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}