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]