Detailed description of Java scheduled task Timer schedule
Do not speak gossip. Go to the demo directly.
[Java]View plaincopy
- Public class TimerTask {
- Public static int I = 1;
- Public static class TestTask extends TimerTask {
- Public void run (){
- System. out. println ("the" + I ++ "times scheduled task ");
- }
- }
- Public static void main (String [] args) throws ParseException {
- Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). parse ("10:27:00 ");
- Date firstTime = date;
- Long delay = 6000;
- Long period = 3000;
- Timer timer1 = new Timer ();
- Timer timer2 = new Timer ();
- Timer timer3 = new Timer ();
- Timer timer4 = new Timer ();
- TestTask task = new TestTask ();
- // Timer1.schedule (task, date );
- // Timer2.schedule (task, delay );
- // Timer3.schedule (task, firstTime, period );
- Timer4.schedule (task, delay, period );
- }
- }
The Code defines a scheduled task TestTask and creates four timers. Each timer uses the schedule overload method.
Timer1 is used to execute the scheduled task at the specified time date.
Timer2 executes the scheduled task after the delay of delay in milliseconds.
Timer3 starts at firstTime and runs the scheduled task once every interval of period milliseconds.
Timer4 is a scheduled task executed after the delay of delay in milliseconds, and is executed once every interval of period milliseconds.