-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoffeeShopThread.java
More file actions
33 lines (27 loc) · 927 Bytes
/
Copy pathCoffeeShopThread.java
File metadata and controls
33 lines (27 loc) · 927 Bytes
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
// Copyright: Ganesh Samarthyam, 2016; www.codeops.tech
import java.util.concurrent.*;
class CoffeeShopThread extends Thread {
private Exchanger<String> sillyTalk;
public CoffeeShopThread(Exchanger<String> args) {
sillyTalk = args;
}
public void run() {
String reply = null;
try {
// exchange the first messages
reply = sillyTalk.exchange("Who's there?");
// print what Duke said
System.out.println("Duke: " + reply);
// exchange second message
reply = sillyTalk.exchange("Duke who?");
// print what Duke said
System.out.println("Duke: " + reply);
// there is no message to send, but to get a message from Duke thread,
// both ends should send a message; so send a "dummy" string
reply = sillyTalk.exchange("");
System.out.println("Duke: " + reply);
} catch(InterruptedException ie) {
System.err.println("Got interrupted during my silly talk");
}
}
}