爲suggested by axtavt我創建的FutureTask
一個子類:
public class FutureCallable<V> extends FutureTask<V> {
private Callable<V> callable;
public FutureCallable(Callable<V> callable) {
super(callable);
this.callable = callable;
}
public Callable<V> getCallable() {
return callable;
}
}
,並推翻我ThreadPoolExecutor
子類的幾種方法:
public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
private Set<FutureCallable<UpdateResult>> running = new HashSet<FutureCallable<UpdateResult>>();
@Override
public <T> FutureCallable<T> newTaskFor(Callable<T> callable) {
return new FutureCallable<T>(callable);
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
FutureCallable<UpdateResult> task = (FutureCallable<UpdateResult>) r;
running.add(task);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
FutureCallable<UpdateResult> task = (FutureCallable<UpdateResult>) r;
running.remove(task);
}
...
}
可能會有幫助:http://stackoverflow.com/questions/6933219 /如何到獲取到futuretask-執行狀態。您想要如何精確跟蹤任務的狀態? – zeller