-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureThread.java
More file actions
59 lines (45 loc) · 1.53 KB
/
FutureThread.java
File metadata and controls
59 lines (45 loc) · 1.53 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
package com.leetcode.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class FutureThread {
/***
* 打车的时候要获取价格 ----> {100ms}A--->B{100ms}--->C{100ms} 500ms+50ms 550ms 200ms 150ms
*
*
* 10w --->
*/
private static ExecutorService executorService = Executors.newFixedThreadPool(256);
public static void main(String[] args)throws Exception {
List<Future<Integer>> futureList = new ArrayList<>();
int [] nums ={10,20,30,40,50};
Future<Integer> future = null;
long t1 = System.currentTimeMillis();
for(int val:nums){
future = getCalculate(val);
futureList.add(future);
}
for(Future<Integer> async:futureList){
async.get();
}
System.out.println("耗时:"+(System.currentTimeMillis()-t1)+"ms");
futureList.clear();
nums =new int[]{60,70,80,90,100,200,300,400,500,600,700,800,900,1000,1100};
t1 = System.currentTimeMillis();
for(int val:nums){
future = getCalculate(val);
futureList.add(future);
}
for(Future<Integer> async:futureList){
async.get();
}
System.out.println("耗时:"+(System.currentTimeMillis()-t1)+"ms");
executorService.shutdownNow();
}
public static Future<Integer> getCalculate(int num){
return executorService.submit(()->{
Thread.sleep(100);
return num*num;
});
}
}