-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureTaskTest2.java
More file actions
55 lines (44 loc) · 1.26 KB
/
Copy pathFutureTaskTest2.java
File metadata and controls
55 lines (44 loc) · 1.26 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
package four;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author yangxp
* 2019.12.6
* <p> Future get 会阻塞主线程 直到获取结果
* <p> 会根据调用get的先后顺序来返回结果
*/
public class FutureTaskTest2 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService es1 = Executors.newCachedThreadPool();
Future<String> task2 = es1.submit(new MyThread04());
Future<String> task1 = es1.submit(new MyThread03());
es1.shutdown();
System.out.println(task1.get());
System.out.println(task2.get());
}
}
class MyThread03 implements Callable<String> {
@Override
public String call() throws Exception {
int sum = 999;
for (int i = 0; i < 10; i++) {
Thread.sleep(300);
sum += i;
}
return String.valueOf(sum + 300000000);
}
}
class MyThread04 implements Callable<String> {
@Override
public String call() throws Exception {
int sum = 0;
for (int i = 0; i < 10; i++) {
Thread.sleep(200);
sum += i;
}
return String.valueOf(sum + 300000000);
}
}