In the system provided to the customer, several timers are executed at regular intervals.
However, the customer reported that some data was not successful, so the log showed that the task was terminated because Timer reported an error.
Then you need to execute the task again when Timer reports an error, so add the code in the catch, but it is not very good.
Find
Java. util. concurrent. ScheduledExecutorService;
So let's take a look at how to modify the original Timer code
Public static ScheduledExecutorService executorService = Executors. newScheduledThreadPool (1);/*** execute once every 1 minute */public static void runTimer () {executorService. scheduleAtFixedRate (new Runnable () {@ Overridepublic void run () {try {.....} catch (Exception e) {e. printStackTrace () ;}}, 0, 60, TimeUnit. SECONDS );}
In this way, the desired result is achieved.
Note: you must add try catch to catch an exception before you can continue to execute the next task after the exception. Otherwise, the task will suspend and do nothing.