-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessorExecuteService.java
More file actions
81 lines (70 loc) · 1.85 KB
/
Copy pathProcessorExecuteService.java
File metadata and controls
81 lines (70 loc) · 1.85 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package processor;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
/**
* 多线程并行执行及调度管理服务
* Created by focus on 2018/3/16.
*/
public class ProcessorExecuteService {
Future future;
ProcessorQueue chain;
Map<String,Object> responseResult = new ConcurrentHashMap<>();
public ProcessorExecuteService(List<IProcessor> processors){
chain = new ProcessorQueue(this);
for(IProcessor processor : processors){
chain.addProcessor(processor);
}
}
void addResult(String id,Object o){
this.responseResult.put(id, o);
}
/**
* 获取任务执行完成之后的结果
* @param id
* @return
*/
public Object getProcessorResult(String id){
if(future != null && !future.isDone()){
try {
future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return responseResult.get(id);
}
/**
* 任务执行: 此方法完成之后,所有任务都已经调用完成
* @return
*/
public void execute(){
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new ProcessorAction(chain));
}
/**
* 异步执行任务
*/
public Future executeAsync(){
ForkJoinPool pool = new ForkJoinPool();
future = pool.submit(new ProcessorAction(chain));
return future;
}
/**
* 释放资源
* @return
*/
public boolean close(){
chain.clear();
chain = null;
if(future != null){
future.cancel(true);
future = null;
}
responseResult.clear();
responseResult = null;
return true;
}
}