简体   繁体   English

为什么我的线程在捕获异常时不创建一个新线程?

[英]Why doesn't my Thread create a new one when it catches an Exception?

So I have a thread running like this:所以我有一个像这样运行的线程:

Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("redo groupMonitor ... ");
                            if (redogmSafer < 1) {
                                groupMonitor.run(remoteHost, port);
                            } else {
                            }
                            redogmSafer = 100;
                        }
                    };
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, delayStart, TimeUnit.MILLISECONDS);

if (redogmSafer < 1) {
} else {
    service.shutdown();
    service.shutdownNow();
    redogmSafer = 0;
}

And I want to run() the Thread again, after it has exited due to an exception or else(Happens all 4-5 Hours).我想再次run() Thread ,在它因异常或其他原因退出后(发生所有 4-5 小时)。

I have tried to shutdown() and shutdownNow() , but that doesn't help either.我曾尝试shutdown()shutdownNow() ,但这也无济于事。 It's like Java doesn't want to redo Threads once it has started been started and shutdown...就像 Java 不想在线程启动和关闭后重做线程......

You can create a new Thread like this:您可以像这样创建一个新线程:

Thread newThread = new Thread(runnable);

newThread.start();

or use an ExecutorService:或使用 ExecutorService:

ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(runnable);

But you are already using a ScheduledExecutorService which should do the job.但是您已经在使用应该完成这项工作的 ScheduledExecutorService。

Wrap your Runnable / Callable code with try-catch用 try-catch 包装您的Runnable / Callable代码

A ScheduledExecutorService halts further scheduling upon receiving a Throwable (an Exception or Error ). ScheduledExecutorService在接收到ThrowableExceptionError )时会停止进一步的调度。 This halting is silent, with no error reported, and no messages or logging.这种停止是无声的,没有错误报告,也没有消息或日志记录。

So you should be looking at prevention of the problem rather than a fix.因此,您应该着眼于预防问题而不是解决问题。 The prevention is to wrap your Runnable / Callable code with a try-catch.预防措施是用 try-catch 包装您的Runnable / Callable代码。

As long as no Throwable bubbling up the call stack reaches the scheduled executor service, the scheduling continues.只要没有Throwable冒泡调用堆栈到达调度的执行器服务,调度就会继续。

Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try{
                                System.out.println("redo groupMonitor ... ");
                                if (redogmSafer < 1) {
                                    groupMonitor.run(remoteHost, port);
                                } else { 
                                    …
                                }
                                redogmSafer = 100;
                            } catch ( Exception e ) { … }
                        }
                    };

Whether you catch Throwable , Exception , or some more specific exception(s) is up to you and your team to decide as appropriate to your situation.是否捕获ThrowableException或一些更具体的异常取决于您和您的团队根据您的情况来决定。

This topic has been addressed multiple times already on Stack Overflow.这个话题已经在 Stack Overflow 上讨论过多次。 Search to learn more.搜索以了解更多信息。 Specifically, see ScheduledExecutorService Exception handling .具体来说,请参阅ScheduledExecutorService 异常处理

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

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