简体   繁体   English

如何停止下一个线程在ScheduledThreadPoolExecutor中运行

[英]How to stop next thread from running in a ScheduledThreadPoolExecutor

I have a ScheduledThreadPoolExecutor which has one thread and runs for every 30 seconds. 我有一个ScheduledThreadPoolExecutor,它有一个线程,每30秒运行一次。

Now, if the current executing thread throws some exception, then I need to make sure that the next thread do not run and the the ScheduledThreadPoolExecutor is down. 现在,如果当前正在执行的线程引发某些异常,那么我需要确保下一个线程不运行并且ScheduledThreadPoolExecutor处于关闭状态。

How do I achieve this? 我该如何实现?

As a clean way, you can simply use a static accessed class to set/check the execution availability. 作为一种干净的方法,您可以简单地使用静态访问的类来设置/检查执行可用性。

import java.util.concurrent.atomic.AtomicBoolean;

class ThreadManager
{
    private static AtomicBoolean shouldStop = new AtomicBoolean(false);

    public static void setExceptionThrown(boolean val)
    {
        shouldStop.set(val);
    }

    public boolean shouldExecuteTask()
    {
        return !shouldStop.get();
    }
}

And a custom runnable implementation that allows you to check for the possibility to execute the task 还有一个自定义的可运行实现,使您可以检查执行任务的可能性

abstract class ModdedRunnable implements Runnable
{
    @Override
    public void run()
    {
        if(ThreadManager.shouldExecuteTask())
        {
            try
            {
                runImpl();
            }
            catch(Exception t)
            {
                ThreadManager.setExceptionThrown(true);
            }
        }
    }

    public abstract void runImpl() throws Exception;
}

Catch the exception call shutdown/shutdownNow API in ExecutorService ExecutorService中捕获异常调用shutdown / shutdownNow API

shutdown() 关掉()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. 启动有序关闭,在该关闭中执行先前提交的任务,但不接受任何新任务。 Invocation has no additional effect if already shut down. 如果已关闭,则调用不会产生任何其他影响。 This method does not wait for previously submitted tasks to complete execution. 此方法不等待先前提交的任务完成执行。 Use awaitTermination to do that . 使用awaitTermination可以做到这一点

shutdownNow() shutdownNow()

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. 尝试停止所有正在执行的任务,暂停正在等待的任务的处理,并返回正在等待执行的任务的列表。 This method does not wait for actively executing tasks to terminate. 此方法不等待主动执行的任务终止。 Use awaitTermination to do that . 使用awaitTermination可以做到这一点

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. 除了尽最大努力尝试停止处理正在执行的任务之外,没有任何保证。 For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate. 例如,典型的实现将通过Thread.interrupt()取消,因此任何无法响应中断的任务都可能永远不会终止。

Refer to these post for more details with working code. 有关工作代码的更多详细信息,请参阅这些帖子。

How to forcefully shutdown java ExecutorService 如何强制关闭Java ExecutorService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM