public class Helper {
private ExecutorService service;
public String startService() {
// ExecutorService service = Executors.newSingleThreadExecutor();
service = Executors.newSingleThreadExecutor();
service.submit(new Runnable() {
public void run() {
new Worker().startWork(callableTaskList);
}
});
return "started"
}
public void stopService() {
service.shutdownNow();
}
}
然而,为了使其发挥作用
API
指示可调用/可运行必须行为良好,并且
中断时响应
public class Worker {
private ExecutorService service;
private ExecutorService anotherService;
public void startWork(List<CallableTask> callableTaskList) throws Exception {
service=Executors.newFixedThreadPool(50);
anotherService=Executors.newFixedThreadPool(50);
for (List<CallableTask> partition : Iterables.partition(callableTaskList, 500)){
checkInterruptStatus();
// do some work here and then return
List<Future<String>> futures=service.invokeAll(partition );
for(Future<String> future: futures){
checkInterruptStatus();
anotherService.submit(new Task(future.get()));
}
}
}
private void checkInterruptStatus() throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
}
public void stopService() {
service.shutdownNow();
anotherService.shutdownNow();
}
}