Java thread (4): timer and timertask

Source: Internet
Author: User

Previous Article: Java thread (3)

Timer and timertask can be used as the third way to implement the thread. The first two methods inherit from the Thread class and implement the runnable interface respectively.

Timer is a thread facility used to schedule tasks to be executed in background threads. You can schedule a task to be executed once or execute it regularly. It can be considered as a timer and can schedule timertask. Timertask is an abstract class that implements the runnable interface, so it is capable of multithreading.

A timer can schedule any number of timertasks. It stores timertasks in a queue and schedules them sequentially. If you want to execute two timertasks concurrently, you need to create two timers. Here is a simple example:

Import Java. util. timer; import Java. util. timertask; public class timertest {static class mytimertask1 extends timertask {public void run () {system. out. println ("Explosion !!! ") ;}} Public static void main (string [] ARGs) {timer = new timer (); timer. schedule (New mytimertask1 (), 2000); // start the task in two seconds }}

Schedule is the method used by timer to schedule tasks. Timer restructured four schedule methods. For details, see the jdk api.

Let's look at a slightly complex example. Suppose there is such a need to implement a chain bomb, which explodes once two seconds later and once three seconds later, so that the loop goes on, in this case, you need to create two tasks for mutual scheduling. The Code is as follows:

Import Java. util. date; import Java. util. timer; import Java. util. timertask; public class timertest {static class mytimertask1 extends timertask {public void run () {system. out. println ("Explosion !!! "); New timer (). schedule (New mytimertask2 (), 2000) ;}} static class mytimertask2 extends timertask {public void run () {system. out. println ("Explosion !!! "); New timer (). schedule (New mytimertask1 (), 3000) ;}} public static void main (string [] ARGs) {timer = new timer (); timer. schedule (New mytimertask2 (), 2000); While (true) {system. out. println (new date (). getseconds (); try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}

Next article: Java threads (V)

This article is from: Gao Shuang | coder, original address: http://blog.csdn.net/ghsau/article/details/7436458.

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.