- Package com.lid;
- Import Java.util.Calendar;
- Import Java.util.Date;
- Import Java.util.Timer;
- Import Java.util.TimerTask;
- Public class Test {
- public static void Main (string[] args) {
- //timer1 ();
- Timer2 ();
- //timer3 ();
- //timer4 ();
- }
- ///The first method: sets the specified task to execute the schedule (timertask task, Date time) at the specified times
- public static void Timer1 () {
- Timer timer = new timer ();
- Timer.schedule (new TimerTask () {
- public Void Run () {
- System.out.println ("-------set to specify task--------");
- }
- }, 2000); //Set the specified time, 2000 milliseconds
- }
- ///The second method: sets the execution of a fixed delay peroid for a specified task after a specified delayed delay
- //Schedule (timertask task, long delay, long period)
- public static void Timer2 () {
- Timer timer = new timer ();
- Timer.schedule (new TimerTask () {
- public Void Run () {
- System.out.println ("-------set to specify task--------");
- }
- }, + , 1000);
- }
- ///Third method: Sets the execution of a fixed frequency peroid for the specified task tasks after the specified delayed delay.
- //Scheduleatfixedrate (timertask task, long delay, long period)
- public static void Timer3 () {
- Timer timer = new timer ();
- Timer.scheduleatfixedrate (new TimerTask () {
- public Void Run () {
- System.out.println ("-------set to specify task--------");
- }
- }, + , 2000);
- }
- //Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat.
- //Timer.scheduleatfixedrate (TimerTask task,date firsttime,long period)
- public static void Timer4 () {
- Calendar calendar = Calendar.getinstance ();
- Calendar.set (Calendar.hour_of_day, 12); //Control time
- Calendar.set (Calendar.minute, 0); //Control points
- Calendar.set (Calendar.second, 0); //control seconds
- Date time = Calendar.gettime (); //arrive at the time of the execution of the task, here is today's 12:00:00
- Timer timer = new timer ();
- Timer.scheduleatfixedrate (new TimerTask () {
- public Void Run () {
- System.out.println ("-------set to specify task--------");
- }
- }, time, + * ( ); This setting will delay the execution of the daily fixed
- }
- }
Several uses of Java timers