1, Timer
Timer is essentially a multi-thread. It can be seen from its class:
Java code
1. Private timerthread thread = new timerthread (Queue );
It is applicable to time-related operations. After a certain action is executed, an action is executed at intervals. For example, we need to refresh our pointer every second in the clock program. For example, Timer is a good choice to simulate the beat of the heart.
2. Set the timer thread to the background thread.
Java code
1. Public class time {
2. Private Final timer = new timer (true );
3.
4. Public void start (){
5. Timer. Schedule (New timertask (){
6. Public void run (){
7. system. Out. println ("your egg is ready! ");
8 .}
9.}, 1000,100 0 );
10 .}
11.
12. Public static void main (string [] ARGs ){
13. Time eggtimer = new time ();
14. eggtimer. Start ();
15. Try {
16. thread. Sleep (5000 );
17.} catch (interruptedexception e ){
18. E. printstacktrace ();
19 .}
20 .}
21 .}
Public class time {
Private Final timer = new timer (true );
Public void start (){
Timer. Schedule (New timertask (){
Public void run (){
System. Out. println ("your egg is ready! ");
}
}, 1000,100 0 );
}
Public static void main (string [] ARGs ){
Time eggtimer = new time ();
Eggtimer. Start ();
Try {
Thread. Sleep (5000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
After one second, timertask is run every second. The background thread set by timer automatically exits after the main thread exits. Generally, I think there are more and more timer threads and my machine is getting slower and slower. Oh, I'm sure my program is running, before the launch, I found a timer and did not exit. Then I changed all the timer of my program to the background. Timer is generally used to complete a task. Without the front-end thread, it would have no meaning. In my program, timer is used to detect file changes, then notify the front-end program file to change.
3. timer runs for a period of time and is cancel
Java code
1. Public class time {
2. Private Final Static timer = new timer ();
3.
4. Public void start (){
5. Timer. Schedule (New timertask (){
6. Public void run (){
7. system. Out. println ("your egg is ready! ");
8 .}
9.}, 1000,100 0 );
10 .}
11.
12. Public static void main (string [] ARGs ){
13. Time eggtimer = new time ();
14. eggtimer. Start ();
15. Try {
16. thread. Sleep (5000 );
17. Timer. Cancel ();
18.} catch (interruptedexception e ){
19. E. printstacktrace ();
20 .}
21 .}
22 .}
Public class time {
Private Final Static timer = new timer ();
Public void start (){
Timer. Schedule (New timertask (){
Public void run (){
System. Out. println ("your egg is ready! ");
}
}, 1000,100 0 );
}
Public static void main (string [] ARGs ){
Time eggtimer = new time ();
Eggtimer. Start ();
Try {
Thread. Sleep (5000 );
Timer. Cancel ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
4. After timer runs five times, it is cancel
Java code
1. Public class time {
2. Private Final Static timer = new timer ();
3.
4. Public void start (){
5. Timer. Schedule (New timertask (){
6. Private int COUNT = 5;
7. Public void run (){
8. system. Out. println ("your egg is ready! ");
9. If (count -- = 0)
10. Timer. Cancel ();
11 .}
12.}, 1000,100 0 );
13 .}
14.
15. Public static void main (string [] ARGs ){
16. Time eggtimer = new time ();
17. eggtimer. Start ();
18 .}
19 .}
From: http://xmind.javaeye.com/blog/718699