Java timer usage, daemon thread.

Source: Internet
Author: User

Package CN. vicky; </P> <p> Import Java. util. timer; </P> <p>/** <br/> * Timer: implements the timer mbean. The timer mbean sends a warning at the specified time to wake up all registered listeners to receive timer notifications. This class manages a list of expired timer notifications. <Br/> * This is a method that allows users to add or remove notifications as needed. When the timer sends a timer notification and expires, the notification is automatically removed from the timer notification list. You can add other timer notifications to notifications that are repeatedly sent. <Br/> */<br/> public class mytimer {</P> <p> Public static void main (string [] ARGs) {<br/> timer = new timer (); <br/> // run this task after 1 second. The interval is 2 seconds. If you pass a data parameter, you can execute this task at a fixed time. <br/> timer. schedule (New mytask (), 1000,200 0); <br/> // This is used to stop this task, otherwise, the task will be executed cyclically. <br/> try {<br/> thread. sleep (10000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> // use this method to exit the task <br/> timer. cancel (); <Br/>}</P> <p> static class mytask extends Java. util. timertask {<br/> @ override <br/> Public void run () {<br/> system. out. println ("done! "); <Br/>}< br/>

Done!
Done!
Done!
Done!
......

 

 

The implementation of the daemon thread. This is a good and powerful timer function!

Package CN. vicky; </P> <p> Import Java. util. timer; <br/> Import Java. util. timertask; </P> <p> public class mytimer2 {</P> <p> private string name; <br/> private long timeout; </P> <p> private thread; </P> <p> Public mytimer2 (string name, long timeout) {<br/> This. name = Name; <br/> This. timeout = timeout; <br/>}</P> <p>/** <br/> * when the processing thread of a node consumes too long, the timer will call {@ link system # exit system. exit} method to force the transaction processing thread of the node to close. This timer is a daemon process. <br/> * therefore, when a service is properly closed, the task does not run this program. <Br/> */<br/> Public void startshutdowntimeout (long timeout) {<br/> New timer (true ). schedule (New timertask () {// true, indicating that the thread to be executed is a daemon thread <br/> @ override <br/> Public void run () {<br/> system. out. println ("Thread" + name + "times out and executes the daemon thread! "); <Br/> // system. exit (1); <br/> thread. stop (); <br/>}< br/>}, timeout); <br/>}</P> <p> Public void mytest () {<br/> thread = new thread () {<br/> @ override <br/> Public void run () {<br/> startshutdowntimeout (timeout ); // This thread is played by the daemon. <br/> system. out. println (name + "begin"); <br/> try {<br/> thread. sleep (200); // The function must be executed for at least 200 milliseconds <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> system. out. println (name + "over"); <br/>}< br/>}; <br/> thread. start (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> mytimer2 M1 = new mytimer2 ("1-", 100); <br/> mytimer 2 m2 = new mytimer2 ("2-", 180 ); <br/> mytimer 2 m3 = new mytimer2 ("3-", 250); <br/> mytimer2 M4 = new mytimer2 ("4-", 320 ); </P> <p> m1.mytest (); <br/> m2.mytest (); <br/> m3.mytest (); <br/> m4.mytest (); <br/>}< br/>

2-begin
3-begin
4-begin
1-begin
Thread 1-Time-out, run the daemon thread!
Thread 2-Timeout, run the daemon thread!
3-over
4-over

 

Package CN. vicky; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. timer; </P> <p>/* <br/> * This class provides the main methods for using timer and timertaske, including customizing tasks and adding and exiting tasks, exit the timer. <br/> * because the Status field of timertask is package-level accessible, there is no way in Java. util. the status is obtained outside the package, which causes some inconvenience to programming. <br/> * the status of a task cannot be determined. <br/> */<br/> public class mytimer3 {</P> <p> Public static void main (string [] ARGs) {<br/> timer = new timer (); <br/> mytask mytask1 = new mytask (); <br/> mytask mytask2 = new mytask (); <br/> mytask2.setinfo ("myTask-2"); <br/> timer. schedule (mytask1, 1000,500 0); <br/> timer. scheduleatfixedrate (mytask2, 2000,500 0); <br/> while (true) {<br/> try {<br/> byte [] info = new byte [1024]; <br/> int Len = system. in. read (Info); <br/> string strinfo = new string (Info, 0, Len, "GBK"); // read information from the console <br/> If (strinfo. charat (strinfo. length ()-1) = '') {<br/> strinfo = strinfo. substring (0, strinfo. length ()-2); <br/>}< br/> If (strinfo. startswith ("cancel-1") {<br/> // exit a single task <br/> mytask1.cancel (); <br/> // you should check whether mytask2 has exited. If yes, break should be used. however, you cannot obtain the <br/> // mytask2 status outside the package. Therefore, you cannot determine whether to exit the loop. <br/>} else if (strinfo. startswith ("cancel-2") {<br/> mytask2.cancel (); <br/>} else if (strinfo. startswith ("cancel-all") {<br/> // exit timer <br/> timer. cancel (); <br/> break; <br/>}else {<br/> // only judge mytask1, stealing laziness ^ _ ^ <br/> mytask1.setinfo (strinfo); <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> static class mytask extends Java. util. timertask {<br/> string info = "cmd_^"; </P> <p> @ override <br/> Public void run () {<br/> system. out. println (Info); <br/>}</P> <p> Public String getinfo () {<br/> return Info; <br/>}</P> <p> Public void setinfo (string info) {<br/> this.info = Info; <br/>}</P> <p >}< br/>

Pai_^
MyTask-2
Pai_^
MyTask-2
Oy // The input content will replace the original
Oy

MyTask-2
Oy

MyTask-2
Oy

 

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.