Java Timer timetask

Source: Internet
Author: User

Package Com.alan.timer;

Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.Timer;
Import Java.util.TimerTask;
/**
* Java Timer
* @author Shaohaixiong
* @date September 27, 2016 5:38:19
* @version v1.0
*/
public class Timertest {
The first method: set the specified task tasks to execute schedule (timertask task, Date time) at 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 time specified, here is 2000 milliseconds
}

The second method: set the execution of a fixed delay peroid for the specified task tasks after specifying delay delays
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, 5000);
}

The third method: Set the execution of the fixed frequency peroid of the specified task tasks after specifying the 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--------");
}
}, 1000, 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 (); The time to execute 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, 1000 * 60 * 60 * 24);//This setting will be delayed daily fixed execution
}

/**
* Once daily 17:00
*
* @author Shaohaixiong
* @date September 27, 2016 10:08:44
* @version v1.0
*/
public static void Timer5 () {
Calendar calendar = Calendar.getinstance ();
Calendar.set (Calendar.hour_of_day, 17); Control time
Calendar.set (calendar.minute, 0); Control points
Calendar.set (Calendar.second, 0); Control seconds

Date time = Calendar.gettime (); The time to execute the task, here is today's 17:00:00

Timer timer = new timer ();
Timer.scheduleatfixedrate (New TimerTask () {
public void Run () {
System.out.println ("-------perform--------17:00 daily");
}
}, time, 1000 * 60 * 60 * 24);//This setting will be delayed daily fixed execution
}
private static final int day=1;//Day
/**
* Executed once per month at number 1th
*
* @author Shaohaixiong
* @date September 27, 2016 10:11:26
* @version v1.0
*/
public static void Timer6 () {
Final Calendar calendar = Calendar.getinstance ();
Calendar.set (calendar.hour_of_day, 0); Control time
Calendar.set (calendar.minute, 0); Control points
Calendar.set (Calendar.second, 0); Control seconds
Date time = Calendar.gettime ();
Timer timer = new timer ();
Timer.scheduleatfixedrate (New TimerTask () {
public void Run () {
Determine if the current month is number 1th
if (Calendar.get (calendar.day_of_month) ==day) {
System.out.println ("-------execution--------1th per month");
}
}
}, time, 1000 * 60 * 60 * 24);//This setting will be delayed daily fixed execution
}
public static void Main (string[] args) {
Timer1 ();
Timer2 ();
Timer3 ();
Timer4 ();
Timer5 ();
Timer6 ();
}
}

Java Timer timetask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.