forked from wesleyegberto/java-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferQueueExample.java
More file actions
158 lines (136 loc) · 3.33 KB
/
TransferQueueExample.java
File metadata and controls
158 lines (136 loc) · 3.33 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
import java.util.concurrent.TimeUnit;
public class TransferQueueExample {
public static void main(String[] args) throws Exception {
TransferQueue<String> queue = new LinkedTransferQueue<>();
var producer = new Producer(queue);
var consumer = new Consumer(queue);
new Thread(consumer).start();
new Thread(producer).start();
Thread.currentThread().join();
}
}
class Producer implements Runnable {
private TransferQueue<String> queue;
private Scanner input;
Producer(TransferQueue<String> queue) {
this.queue = queue;
this.input = new Scanner(System.in);
}
public void run() {
log("started");
while (true) {
log("=== Enter the number: 1 - wait; 2 - wait with timeout; 3 - transfer timedout");
var command = input.nextLine();
switch (command) {
case "1":
send("wait");
break;
case "2":
send("timeout");
break;
case "3":
send("transfer_timeout");
waitAndSendWithTimeout("command will not be received");
break;
default:
log("Invalid command");
}
}
}
private void send(String command) {
try {
log("Sending command");
queue.transfer(command);
log("Command sent and received");
} catch (Exception ex) {
log(ex.getMessage());
}
}
private void sendWithTimeout(String command) {
try {
boolean received = queue.tryTransfer(command, 3, TimeUnit.SECONDS);
log("Command sent, received: " + received);
} catch (Exception ex) {
log(ex.getMessage());
}
}
private void waitAndSendWithTimeout(String command) {
try {
log("Sleeping...");
Thread.sleep(1000);
log("Sending command waiting for 3s");
boolean received = queue.tryTransfer(command, 3, TimeUnit.SECONDS);
log("Command sent, received: " + received);
} catch (Exception ex) {
log(ex.getMessage());
}
}
private void log(String message) {
System.out.printf("[Producer] %s%n", message);
}
}
class Consumer implements Runnable {
private BlockingQueue<String> queue;
Consumer(BlockingQueue<String> queue) {
this.queue = queue;
}
public void run() {
log("started");
String command = waitIndefinitely();
while (true) {
switch (command) {
case "wait":
command = waitIndefinitely();
break;
case "timeout":
command = waitWithTimeout(5);
break;
case "transfer_timeout":
sleep();
command = waitWithTimeout(5);
break;
default:
log("Invalid command");
command = waitIndefinitely();
}
}
}
private String waitIndefinitely() {
log("Waiting...");
try {
String command = queue.take();
log("Command received: " + command);
return command;
} catch (Exception ex) {
log(ex.getMessage());
return null;
}
}
private String waitWithTimeout(long seconds) {
log("Waiting " + seconds + "s");
try {
String command = queue.poll(seconds, TimeUnit.SECONDS);
if (command == null) {
log("Timedout");
return "";
}
log("Command received: " + command);
return command;
} catch (Exception ex) {
log(ex.getMessage());
return null;
}
}
private void sleep() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {}
}
private void log(String message) {
System.out.printf("[Consumer] %s%n", message);
}
}