Java Timer Timer Usage

Source: Internet
Author: User

Java comes with a timer that has two important classes: TimerTask and timer. As follows:

Simple to use:

Package Com;import Java.util.date;import Java.util.timer;public class Timertest extends timer{public static void main ( String[] args) throws interruptedexception {Task task = new Task (); Timer QUARTZ2 = new Timer ("task", true);//Daemon thread
Quartz2.schedule (Task, New Date ()); System.out.println ("***************************"); Thread.Sleep (10);//main thread hibernation 10ms, the task took the opportunity to execute. }}


Package Com;import Java.util.timertask;public Class Task extends TimerTask {int i = 0; @Overridepublic void Run () {SYSTEM.O UT.PRINTLN ("Thread   " + thread.currentthread (). GetId () + ", \ T" + thread.currentthread (). GetName () + "\ t start execution ... ") while (true) {System.out.println (" executed: \ t "+ i +" \ T "), i++;if (i = =) {System.out.println (" ===================== "+ I ), Break;}}}


Execution Result:

Java Timer Timer Usage

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.