0

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

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 8
    That's correct. Once the thread terminated you can't start it again. But what is the problem since you use `scheduleAtFixedRate`? The task to execute get's executed again later on. Please [edit] your question to include the full source code you have as a [mcve]. – Progman Jul 12 '20 at 16:55
  • Perhaps you should explain `groupMonitor.run(remoteHost, port)`. – Basil Bourque Jul 12 '20 at 21:38

2 Answers2

0

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.

0

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • so once the method groupMonitor.run() gets an exception it will get called again, right? – aqwertyyyz Jul 12 '20 at 19:32
  • i need the runnable to call groupMonitor.run() each time the method returns because of an exception like connection lost ETC. – aqwertyyyz Jul 12 '20 at 19:33
  • @aqwertyyyz Your `Runnable` object’s `run` method will run every so often, after elapsing the amount of time you specify in your call to `scheduleAtFixedRate`. If you pass 500 milliseconds, then every half-second the `Runnable` object’s `run` method executes. In your case, if your `if` test passes, `groupMonitor.run` executes. What that method does, I cannot address as you do not explain `groupMonitor.run(remoteHost, port)`. But I can say that if during one of the executions of that method an `Exception` is thrown and not caught, that will be the last execution of the runnable’s `run`. – Basil Bourque Jul 12 '20 at 21:35
  • i will update the answer to add more clarity to the3 methods work – aqwertyyyz Jul 14 '20 at 08:43