Use the scheduled task ScheduledThreadPoolExecutor that comes with java

ScheduledThreadPoolExecutor is a subclass of ThreadPoolExecutor;

This is what the JDK api says:

 

ThreadPoolExecutor, which additionally schedules the command to run after a given delay, or executes the command periodically. This class is preferred when multiple worker threads are required, or when  ThreadPoolExecutor additional flexibility or functionality is required  Timer.

A deferred task is executed as soon as it is enabled, but there are no real-time guarantees about when it is enabled and when it is enabled. Tasks that are scheduled to execute at the same time are enabled in first-in, first-out (FIFO) order of submission.

---------------

Usually when we perform a timed task, we will use Time, and TimeTask to combine processing;

But Timer and TimerTask have some flaws:

1: Timer only creates one thread. When your task execution time exceeds the set delay time will cause some problems.

2: The thread created by Timer does not handle exceptions, so once an unchecked exception is thrown, the thread terminates immediately.

ScheduledThreadPoolExecutor is recommended after JDK 5.0. This class belongs to the Executor Framework. In addition to handling exceptions, it can also create multiple threads to solve the above problems

 

 

copy code
1 package timer;
 2
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 import java.util.concurrent.ScheduledThreadPoolExecutor;
 6 import java.util.concurrent.TimeUnit;
 7
 8 public class Test
 9 {
10     static ScheduledThreadPoolExecutor stp = null;
11     static int index;
12     
13     private static String getTimes() {  
14         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");  
15         Date date = new Date();  
16         date.setTime(System.currentTimeMillis());  
17         return format.format(date);  
18     }
19     
20     
21     private static class MyTask implements Runnable {  
22         
23         @Override  
24         public void run() {  
25             index++;  
26             System.out.println("2= " + getTimes()+" "  +index);  
27 //            if(index >=10){  
28 //                stp.shutdown();  
29 //                if(stp.isShutdown()){  
30 // System.out.println("Stopped????");
31 //                }
32 //            }
33         }
34     }
35     public static void main(String[] args)
36     {
37         stp = new ScheduledThreadPoolExecutor(5);
38         MyTask mytask = new MyTask();
39 //mytask is the thread, 2 is the delay time of the first execution, and the last parameter is the time unit
40 //        stp.schedule(mytask, 2, TimeUnit.SECONDS);
41 // The first execution delay is 2 seconds, and the subsequent execution cycle is 1 second
42 //        stp.scheduleAtFixedRate(mytask, 2, 1,TimeUnit.SECONDS );
43 //The first execution delay is 2 seconds, then 1 second from the end of the previous task to the start of the next task
44         stp.scheduleWithFixedDelay(mytask, 2, 1, TimeUnit.SECONDS);
45         
46     }
47
48 }
copy code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815175&siteId=291194637