-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompletableExam.java
38 lines (31 loc) · 1.13 KB
/
CompletableExam.java
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
import java.util.Timer;
import java.util.concurrent.*;
import java.util.function.Function;
/**
* Created by ran on 02/06/2015.
*/
public class CompletableExam {
public static void main(String[] args) {
ExecutorService executor1 = Executors.newFixedThreadPool(2);
ExecutorService executor2 = Executors.newFixedThreadPool(2);
final CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < 1000)
System.out.println("aaa");
return "8";
}, executor1);
final CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < 1000)
System.out.println("bbb");
return "65";
}, executor2);
ddasa
future2.join();
future1.join();
future1.thenAccept(System.out::println);
future2.thenAccept(System.out::println);
executor1.shutdown();
executor2.shutdown();
}
}