forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT23.java
More file actions
39 lines (32 loc) · 1004 Bytes
/
T23.java
File metadata and controls
39 lines (32 loc) · 1004 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
33
34
35
36
37
38
39
package com.basic;
import java.util.concurrent.*;
/**
* @program JavaBooks
* @description: future
* @author: mf
* @create: 2020/01/01 14:18
*/
public class T23 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> task = new FutureTask<>(() -> {
TimeUnit.MILLISECONDS.sleep(500);
return 1000;
});
new Thread(task).start();
System.out.println(task.get()); // 阻塞
ExecutorService service = Executors.newFixedThreadPool(5);
Future<Integer> f = service.submit(() -> {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
});
// System.out.println(f.get());
System.out.println(f.isDone());
System.out.println(f.get());
System.out.println(f.isDone());
service.shutdown();
}
}