Timed call in java code

Posted by dancer on Mon, 13 Apr 2020 18:23:11 +0200

(1) There are three methods of timing call in java code:

Method 1: use thread
Create a thread and keep it running in the while loop. Use the sleep method to achieve the effect of timing tasks. The code is as follows:

  1. public static void main(String[] args) {  
  2.     final long timeInterval = 1000;  
  3.     Runnable runnable = new Runnable() {  
  4.         public void run() {  
  5.             while (true) {  
  6.                 System.out.println("Method 1: cyclic call !!  time=" + new Date() );  
  7.                 try {  
  8.                     Thread.sleep(timeInterval);  
  9.                 } catch (InterruptedException e) {  
  10.                     e.printStackTrace();  
  11.                 }  
  12.             }  
  13.         }  
  14.     };  
  15.     Thread thread = new Thread(runnable);  
  16.     thread.start();  
  17. }  

Method 2: in implementation, timer class can schedule tasks, while TimerTask implements specific tasks in run() method. Timer instance can schedule multiple tasks. It is thread safe.
When Timer's constructor is called, it creates a thread that can be used to schedule tasks.  

For details of Timer address, you can view the call time at http://blog.csdn.net/chenssy/article/details/3273499

Here is the simple code:

  1. public static void main(String[] args) {  
  2.     TimerTask task = new TimerTask() {  
  3.         @Override  
  4.         public void run() {  
  5.             System.out.println("Method two: Timer Timed call !!!  time=" + new Date() );  
  6.         }  
  7.     };  
  8.     Timer timer = new Timer();  
  9.     long delay = 10000;  
  10.     long intevalPeriod = 1 * 1000;  
  11.     timer.scheduleAtFixedRate(task, delay, intevalPeriod);  
  12. }  

Method 3: schedulexecutiorservice is introduced from java.util.concurrent of Java SE5 as a concurrent tool class, which is the most ideal way to implement timed tasks. ScheduledThreadPoolExecutor can be said to be the multithreaded implementation version of Timer, which is recommended by JDK.

Please refer to http://blog.csdn.net/guozebo/article/details/51090612

Here is the simple code:


  1. public static void main(String[] args) {  
  2.     Runnable runnable = new Runnable() {  
  3.         public void run() {  
  4.             System.out.println("Method three: ScheduledExecutorService Timed call !!  time=" + new Date() );  
  5.         }  
  6.     };  
  7.     ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();  
  8.     //The second parameter is the delay time of the first execution, and the third parameter is the interval time of the scheduled execution
  9.     service.scheduleAtFixedRate(runnable, 5, 3, TimeUnit.SECONDS);  
  10. }  

(2) The example of java code timing call in spring: (the following code is called once every few minutes, and Baidu Library timing call http://wenku.baidu.com/view/a8ac2b71a8114431b80dd809.html)

Write the class TimerManager:

  1. public class TimerManager {  
  2.      public TimerManager() {  
  3.         NFDFlightDataTimerTask task = new NFDFlightDataTimerTask();  
  4.         ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();  
  5.         //The second parameter is the delay time of the first execution, and the third parameter is the interval time of the scheduled execution
  6.         service.scheduleAtFixedRate(task, 5, 3, TimeUnit.SECONDS);  
  7.      }  
  8. }  


  1. public class NFDFlightDataTimerTask extends TimerTask{  
  2.      private static Logger log = Logger.getLogger(NFDFlightDataTimerTask.class);  
  3.      @Override  
  4.      public void run() {  
  5.           try {  
  6.               //Write what you want to do here
  7.               System.out.println("Perform scheduled call !!  time=" + new Date() );  
  8.           } catch (Exception e) {  
  9.               log.info("-------------Exception in parsing information--------------");  
  10.           }  
  11.      }  
  12. }  

Write the class NFDFlightDataTaskListener:

  1. public class NFDFlightDataTaskListener implements ServletContextListener {  
  2.      public void contextInitialized(ServletContextEvent event) {  
  3.           new TimerManager();  
  4.      }  
  5.      public void contextDestroyed(ServletContextEvent event) {  
  6.      }  
  7. }  

web.xml

  1.     <listener>  
  2.         <listener-class>com.cxy.timer.NFDFlightDataTaskListener</listener-class>  
  3. </listener>  

Topics: Java JDK Spring xml