简体   繁体   中英

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).

I have tried to shutdown() and shutdownNow() , but that doesn't help either. It's like Java doesn't want to redo Threads once it has started been started and shutdown...

You can create a new Thread like this:

Thread newThread = new Thread(runnable);

newThread.start();

or use an ExecutorService:

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

But you are already using a ScheduledExecutorService which should do the job.

Wrap your Runnable / Callable code with try-catch

A ScheduledExecutorService halts further scheduling upon receiving a Throwable (an Exception or Error ). 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.

As long as no Throwable bubbling up the call stack reaches the scheduled executor service, the scheduling continues.

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.

This topic has been addressed multiple times already on Stack Overflow. Search to learn more. Specifically, see ScheduledExecutorService Exception handling .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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