-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSynchronousQueue_1.java
More file actions
63 lines (54 loc) · 1.39 KB
/
SynchronousQueue_1.java
File metadata and controls
63 lines (54 loc) · 1.39 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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
/**
* JAVA MULTITHREAD - SynchronousQueue
*
* @author RinaldoDev
*/
public class SynchronousQueue_1 {
private static final SynchronousQueue<String> FILA =
new SynchronousQueue<>();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
Runnable r1 = () -> {
put();
System.out.println("Escreveu na fila!");
};
Runnable r2 = () -> {
String msg = take();
System.out.println("Pegou da fila! " + msg);
};
executor.execute(r1);
executor.execute(r2);
executor.shutdown();
}
private static String take() {
try {
return FILA.take();
// return FILA.poll(timeout, unit);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
return "Exceção!";
}
}
private static void put() {
try {
FILA.put("Inscreva-se");
// FILA.offer(e, timeout, unit);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
}
//
// picpay.me/RinaldoDev
// apoia.se/rinaldodev
//
// YouTube: RinaldoDev
// Twitter: @rinaldodev
// Blog: rinaldo.dev
// LinkedIn: rinaldodev
//