Java multi-threading learning - timed task scheduling

Timer

Itself is a thread, the most important is to schedule ().

schedule () parameter description:

schedule(TimerTask task, long delay)  //延迟delay毫秒以后执行任务

schedule(TimerTask task, long delay, long period)  // delay after delay milliseconds to perform the task, and go on every execution cycle peroid

schedule(TimerTask task, Date time)  // start at a specified date time mission

schedule(TimerTask task, Date firstTime, long period)  // at the specified date and time began to perform the task ,, execute the cycle continues every peroid

TimerTask

Abstract class that implements the interface Runnale, it inherited methods to achieve run, run method which is the content of the task.

Import the java.util.Calendar;
 Import a java.util.GregorianCalendar;
 Import the java.util.Timer;
 Import java.util.TimerTask; 

public  class TimerTest {
     public  static  void main (String [] args) { 
        the Timer Task = new new the Timer () ; 
        calendar startTask = new new the GregorianCalendar ();    // Create a calendar-based 
        startTask.add (Calendar.SECOND, 10);   // Get the number of seconds of the current time point, and then an increase of 10 seconds based on 
        task.schedule ( new new to MyTask ( ), startTask.getTime (), 1000);    //Task started in 10 seconds after the current time point 

    } 
} 

class to MyTask the extends the TimerTask { 

    @Override 
    public  void RUN () { 
        System.out.println ( "the Hello, World" ); 
    } 
}

Information about the date of the class Date / Calendar and Other Materials: https://www.cnblogs.com/chiweiming/p/11178814.html

Guess you like

Origin www.cnblogs.com/chiweiming/p/11183199.html