Get a call from ThreadPoolTaskExecutor or apply Runnable to Callable

I use ThreadPoolTaskExecutor to complete my tasks, which are Callable implementations . I just want to check the time if the task is still in the pool (monitoring). How to do it? I know that I can get the queue from ThreadPoolExecutor , but how can I run Runnable to Callable?

Basically, I have this called

public interface IFormatter extends Callable<Integer>{
    Long getOrderId();
}

I do it like

ThreadPoolExecutor.submit(new Formatter(order));

And finally, I would like to go through the ExecutorService queue in some asynchronous method and check if a stream with orderId exists.

+1
source share
2

, FutureTask Callable, execute. submit Callable ExecutorService - , Callable API.

FutureTask

class MyFutureTask extends FutureTask<Integer> {
    final IFormatter theCallable;

    public MyFutureTask(IFormatter callable) {
        super(callable);
        theCallable=callable;
    }
    Long getOrderId() {
        return theCallable.getOrderId();
    }
}

threadPoolExecutor.execute(new MyFutureTask(new Formatter(order)));,

:

public static boolean isEnqueued(ThreadPoolExecutor e, Long id) {
    for(Object o: e.getQueue().toArray()) {
        if(o instanceof MyFutureTask && Objects.equals(((MyFutureTask)o).getOrderId(), id))
            return true;
    }
    return false;
}

ExecutorService ( , ). ThreadPoolExecutor, FutureTask ( Java 6), , :

public class MyThreadPoolExecutor extends ThreadPoolExecutor {

    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
                                TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
        TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
            workQueue, threadFactory);
    }
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
        TimeUnit unit, BlockingQueue<Runnable> workQueue,
        RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
            workQueue, handler);
    }
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
        TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
        RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
            workQueue, threadFactory, handler);
    }

    @Override
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        if(callable instanceof IFormatter)
            return (FutureTask<T>)new MyFutureTask((IFormatter)callable);
        return super.newTaskFor(callable);
    }
}

, MyThreadPoolExecutor ThreadPoolExecutor, IFormatter MyFutureTask FutureTask. , ExecutorService, .

+2

, ExecutorService, decorateTask(). , .

0

Source: https://habr.com/ru/post/1668947/


All Articles