Simple to use and learning of java Timer

Here only do some simple recording, please further detailed study
https://www.cnblogs.com/0201zcr/p/4703061.html
https://www.breakyizhan.com/java/4900.html

Timer can be used to doing? ?
A first contact Timer is neon, it is necessary to change the operation timing of a color. Here is the need for a method to change the color of regular call. Perform a certain operation or the like such as every 2 seconds. The most convenient for such an operation, efficient implementation is to use java.util.Timer tools.

Instructions:

  1. Create a Timer object.
  2. Call some way to achieve timing calls.
    As public void schedule(TimerTask task, long delay, long period)
    TimerTask it is just a realization of a class run method, and specific TimerTask need to achieve your own, achieve timing need to call in the run method.
    After the start scheduling delay (ms), after each time scheduling, wait a minimum period (ms) after the start scheduling.
   Timer timer = new Timer();
   timer.schedule(new TimerTask() {
       public void run() {
           System.out.println("换颜色");
       }
   }, 2000, 1000); 
   
换颜色
换颜色
换颜色
换颜色
换颜色
换颜色

Guess you like

Origin blog.csdn.net/qq_40803626/article/details/96484439