-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
59 lines (51 loc) · 1.51 KB
/
Test.java
File metadata and controls
59 lines (51 loc) · 1.51 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.figer;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Test{
public static void main(String[] args) throws Exception{
ExecutorService exec = Executors.newCachedThreadPool();
CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(exec);
int[] index = {1,2,3,4,5,6,7,8};
for(final int i : index){
try {
completionService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("...." + i);
Thread.sleep(1500);
if(i == 4)
throw new RuntimeException("111");
return new Random().nextInt(10);
}
});
}catch (Exception e){
System.out.println("ex");
}
}
int sum = 0;
for(int i=0; i<index.length; i++){
try {
completionService.take().get(2, TimeUnit.SECONDS);
sum ++;
}catch (Exception e){
System.out.println("ex" + i);
}
}
System.out.println("总数为:"+sum);
}
}
class View{
private boolean showErrorMsg;
public boolean isShowErrorMsg() {
return showErrorMsg;
}
public View setShowErrorMsg(boolean showErrorMsg) {
this.showErrorMsg = showErrorMsg;
return this;
}
}