[英]Determining which Callables are currently running in ExecutorService
I'd like to be able to get the Callables that I've submit
ted to an ExecutorService (specifically a ThreadPoolExecutor) which are currently in progress (so they're not available via getQueue()
). 我希望能够将我submit
的Callables submit
给当前正在进行的ExecutorService(特别是ThreadPoolExecutor)(因此它们不能通过getQueue()
)。
I tried creating a subclass to override beforeExecute
, the but Runnable given there turns out to be a FutureTask, not the original Callable that was submitted. 我尝试创建一个子类来覆盖beforeExecute
,但是在那里给出的Runnable原来是FutureTask,而不是提交的原始Callable。
Is there another way to go about determining the active Callables? 还有另一种方法可以确定活动的Callables吗?
Ultimately there's two things I'd like to do with this information: 最后,我想对这些信息做两件事:
You can override AbstractExecutorService.newTaskFor()
, it should give you access to original Runnable
s and Callable
s. 您可以覆盖AbstractExecutorService.newTaskFor()
,它应该允许您访问原始的Runnable
和Callable
s。 Then decorate them with your tracking decorator and pass to super.newTaskFor()
. 然后用跟踪装饰器装饰它们并传递给super.newTaskFor()
。
as suggested by axtavt I created a subclass of FutureTask
: 正如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;
}
}
and overrode a few methods in my ThreadPoolExecutor
subclass: 并在我的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);
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.