-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProviderConsumerTest.java
More file actions
59 lines (52 loc) · 1.63 KB
/
ProviderConsumerTest.java
File metadata and controls
59 lines (52 loc) · 1.63 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
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
class Provider implements Runnable {
public void run() {
Random random = new Random();
try {
for (int i = 0; i < 5; i++) {
int j = random.nextInt();
ProviderConsumerTest.queue.put(j);
System.out.println(Thread.currentThread().getName() + " product: " + j);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
public void run() {
Integer data;
for (int i = 0; i < 5; i++) {
try {
data = ProviderConsumerTest.queue.take();
System.out.println(Thread.currentThread().getName() + " comsume: " + data);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ProviderConsumerTest {
public static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
public static void main(String[] args) {
Provider p1 = new Provider();
Provider p2 = new Provider();
Consumer c1 = new Consumer();
Consumer c2 = new Consumer();
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(c1);
Thread t4 = new Thread(c2);
t1.setName("Provider1");
t2.setName("Provider2");
t3.setName("Consumer1");
t4.setName("Consumer2");
t1.start();
t2.start();
t3.start();
t4.start();
}
}