简体   繁体   English

确定当前在ExecutorService中运行的Callables

[英]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: 最后,我想对这些信息做两件事:

  1. Get the running tasks so they can be displayed in the user interface 获取正在运行的任务,以便它们可以显示在用户界面中
  2. Check if the file referenced by the Callable matches a pattern, and if so, cancel it 检查Callable引用的文件是否与模式匹配,如果匹配,则取消它

You can override AbstractExecutorService.newTaskFor() , it should give you access to original Runnable s and Callable s. 您可以覆盖AbstractExecutorService.newTaskFor() ,它应该允许您访问原始的RunnableCallable 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.

相关问题 从ExecutorService获取当前正在运行的线程对象 - Get currently running thread objects from ExecutorService 在ExecutorService中捕获所有期货/可赎回债券的异常 - Catching Exceptions of all Futures/Callables in an ExecutorService 遍历列表并将可调用对象提交给ExecutorService - Iterating over list and submitting Callables to an ExecutorService FJTask与Future + Callables + ExecutorService - FJTask vs Future+Callables+ExecutorService Java:ExecutorService和Callables,无法捕获异常 - Java: ExecutorService and Callables, unable to catch exception JAVA:提交给ExecutorService的可调用对象与ExecutorService之外的线程之间的执行顺序 - JAVA: Execution Order between Callables submitted to ExecutorService and Threads Outside of the ExecutorService 确定ExecutorService的关闭时间 - Determining shutdown time of ExecutorService libgdx - 确定正在运行的平台 - libgdx - determining which platform is running 如何使用ExecutorService管理未知数量的Callables的返回值? - How to manage return values of an unknown amount of Callables using ExecutorService? Java:带有Callables的ExecutorService:invokeAll()和future.get()-结果顺序正确吗? - Java: ExecutorService with Callables: invokeAll() and future.get() - results in correct order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM