为中断分组线程

时间:2019-03-28 20:35:27

标签: java spring multithreading

我在线程分组方面遇到了一些问题,这是一个中断(取消了一些连续的进程)。我有一个由ThreadPoolTaskExecutor管理的执行点条目,可与方法上的@Async注释一起使用。在异步调用并运行此方法之后,在其中内部,我还有一些new Thread()要完成的另一项异步工作。考虑到当我有一些并行运行的任务需要识别时,我可能会遇到这种情况,这是什么任务,并且给定{strong> task 和它的子线程interrupt() >在任务执行过程中创建的。

我已经研究了有关此问题的一些东西。我可以为ThreadGroup设置一些ThreadPoolTaskExecutor,得到这个组并interrupt()。这可能会停止正在运行的任务和在任务执行过程中创建的线程,但是我需要像 dynamic ThreadGroup这样,具体取决于任务ID,以便不停止其他正在运行的任务,因为设置了{{1} }至ThreadGroup设置给定的所有执行器任务。 访问已创建任务的ThreadPoolTaskExecutor().setThreadGroup()并调用Future<>有助于停止当前任务,但不会阻止 child 线程

future.cancel()
    @Bean
    public TaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("thread-");
        executor.initialize();
        return executor;
    }
    public class Task {
       private Long id;
       //getters and setters
    }
   @Service
    public class AsyncService {

        @Async("taskExecutor")
        public Future<Task> startAsyncProcess(Task task){
            //this thread is not killed by future.cancel()
            new Thread(() -> System.out.println("Some continuous async db write")).start();
            //Some other continuous process, killed by future.cancel()
            return AsyncResult.forValue(task);
        }

    }

我希望对这些线程进行一些完全的中断。也许我需要为此创建一些管理器,并使用 @Service public class TaskService { @Autowired private AsyncService asyncService; public void startProcess(Task task){ Future<Task> future = asyncService.startAsyncProcess(task); // future.cancel(); } } 来生成那些线程,并使用ThreadFactory将它们分成小组?预先感谢!

1 个答案:

答案 0 :(得分:0)

这不是很漂亮,但是可以正常工作:实现类似CancelWithRunnableFuture的类型,您可以提供一个Runnable来... Runnablecancel()内部被调用方法。

@Service
class AsyncService {
    @Async("taskExecutor")
    public Future<Task> startAsyncProcess(Task task) {
        Thread threadToInterruptOnCancel = new Thread(() -> System.out.println("Some continuous async db write"));
        threadToInterruptOnCancel.start();

        return new CancelWithRunnableFuture(
                       AsyncResult.forValue(task),
                       () -> threadToKillOnCancel.interrupt());
    }
}

class CancelWithRunnableFuture<V> implements Future<V> {
    private Future<V> delegate;
    private Runnable runOnCancel;

    public CancelWithRunnableFuture(Future<V> delegate, Runnable runOnCancel) {
        this.delegate = delegate;
        this.runOnCancel = runOnCancel;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        //
        // The Runnable is run here!
        //
        runOnCancel.run();

        return delegate.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return delegate.isCancelled();
    }

    @Override
    public boolean isDone() {
        return delegate.isDone();
    }

    @Override
    public V get() throws InterruptedException, ExecutionException {
        return delegate.get();
    }

    @Override
    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return delegate.get(timeout, unit);
    }

}