Several uses of Java timers [go]

Source: Internet
Author: User

1ImportJava.util.Calendar;
2ImportJava.util.Date;
3ImportJava.util.Timer;
4ImportJava.util.TimerTask;
5
6 PublicclassTest {
7 PublicStaticvoidMain (string[] args) {
8//timer1 ();
9 Timer2 ();
Ten//Timer3 ();
One        //timer4 ();
A}
-
-//The first method: set the specified task tasks to execute schedule (timertask task, Date time) at specified times
the PublicStaticvoidTimer1 () {
-Timer timer =NewTimer ();
-Timer.schedule (NewTimerTask () {
- PublicvoidRun () {
+System.out.println ("-------set to specify task--------");
-}
+}, 2000);//sets the time specified, here is 2000 milliseconds
A}
at
-//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)
- PublicStaticvoidTimer2 () {
-Timer timer =NewTimer ();
-Timer.schedule (NewTimerTask () {
in PublicvoidRun () {
-System.out.println ("-------set to specify task--------");
to}
+}, 1000, 1000);
-}
the
*//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)
Panax Notoginseng PublicStaticvoidTimer3 () {
-Timer timer =NewTimer ();
theTimer.scheduleatfixedrate (NewTimerTask () {
+ PublicvoidRun () {
ASystem.out.println ("-------set to specify task--------");
the}
+}, 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)
- PublicStaticvoidTimer4 () {
theCalendar calendar = Calendar.getinstance ();
-Calendar.set (Calendar.hour_of_day, 12);//Control Time
WuyiCalendar.set (calendar.minute, 0);//Control Points
theCalendar.set (Calendar.second, 0);//Control seconds
-
WuDate time = Calendar.gettime ();//The time to execute the task, here is today's 12:00:00
-
AboutTimer timer =NewTimer ();
$Timer.scheduleatfixedrate (NewTimerTask () {
- Public voidRun () {
-System.out.println ("-------set to specify task--------");
-}
A}, time, 1000 * 60 * 60 * 24);//This setting will delay the execution of the daily fixed
+ }
the}

Several uses of Java timers [go]

Related Article

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.