-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPC.java
More file actions
59 lines (57 loc) · 1.6 KB
/
Copy pathPC.java
File metadata and controls
59 lines (57 loc) · 1.6 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.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PC{
public static void main(String[] args){
final Lock lock = new ReentrantLock();
final BlockingQueue<Character> bq = new ArrayBlockingQueue<Character>(26);
final ExecutorService executor = Executors.newFixedThreadPool(2);
Runnable producer = new Runnable(){
public void run(){
for(char ch='A';ch <= 'Z';ch++){
lock.lock();
try{
while(!bq.offer(ch)){ // full and not insert
lock.unlock();
Thread.sleep(50);
lock.lock(); // reacquire the lock, or reentrant
}
System.out.println(ch + " produced by " + Thread.currentThread());
}catch(InterruptedException ie){
ie.printStackTrace();
}finally{
lock.unlock();
}
}
}
};
executor.execute(producer);
Runnable consumeRunnable = new Runnable(){
public void run(){
char ch = '\0';
do{
try{
lock.lock();
Character c;
while((c = bq.poll()) == null){ // empty, no elements can be polled.
lock.unlock();
Thread.sleep(50);
lock.lock(); // reentrant
}
ch = c.charValue();
System.out.println(ch + " consumed by " + Thread.currentThread());
}catch(InterruptedException ie){
ie.printStackTrace();
}finally{
lock.unlock();
}
}while(ch!='Z');
executor.shutdownNow();
}
};
executor.execute(consumeRunnable);
}
}