Java Threads: Timer and TimerTask

Source: Internet
Author: User

The timer and TimerTask can be used as the third way to implement the thread, the first two methods are inherited from the thread class and implement the Runnable interface respectively.

A timer is a thread facility that is used to schedule tasks that are later performed in a background thread. The task can be scheduled to execute once, or periodically repeated execution, can be regarded as a timer, can dispatch timertask. TimerTask is an abstract class that implements the Runnable interface, so it has the capability of multithreading.

A timer can dispatch any number of timertask, it will store timertask in a queue, order scheduling, if you want two timertask concurrent execution, you need to create two timer. Let's look at a simple example:

[Java]View Plaincopyprint?
  1. Import Java.util.Timer;
  2. Import Java.util.TimerTask;
  3. Public class Timertest {
  4. static class MyTimerTask1 extends TimerTask {
  5. public Void Run () {
  6. System.out.println ("Explosion!!!  ");
  7. }
  8. }
  9. public static void Main (string[] args) {
  10. Timer timer = new timer ();
  11. Timer.schedule (new MyTimerTask1 (), + ); Start task after two seconds
  12. }
  13. }

Schedule is a timer scheduling task, the timer reconstructs four schedule methods, you can view the JDK API.

Look at a slightly more complex example, suppose there is a need to implement a serial bomb, 2 seconds after the explosion once, 3 seconds after the explosion once, so loop, this need to create two tasks, scheduling each other, the code is as follows:

[Java]View Plaincopyprint?
  1. Import Java.util.Date;
  2. Import Java.util.Timer;
  3. Import Java.util.TimerTask;
  4. Public class Timertest {
  5. static class MyTimerTask1 extends TimerTask {
  6. public Void Run () {
  7. System.out.println ("Explosion!!!  ");
  8. new Timer (). Schedule (new MyTimerTask2 (), 2000);
  9. }
  10. }
  11. static class MyTimerTask2 extends TimerTask {
  12. public Void Run () {
  13. System.out.println ("Explosion!!!  ");
  14. new Timer (). Schedule (new MyTimerTask1 (), 3000);
  15. }
  16. }
  17. public static void Main (string[] args) {
  18. Timer timer = new timer ();
  19. Timer.schedule (new MyTimerTask2 (), 2000);
  20. While (true) {
  21. System.out.println (new Date (). getseconds ());
  22. try {
  23. Thread.Sleep (1000);
  24. } catch (Interruptedexception e) {
  25. //TODO auto-generated catch block
  26. E.printstacktrace ();
  27. }
  28. }
  29. }
  30. }

After JDK5, you can use Scheduledthreadpoolexecutor to replace the timer.

Java Threads: Timer and TimerTask

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.