Timer and TimerTask

Source: Internet
Author: User

The timer is a timer tool provided in the JDK that, when used, opens a separate thread outside the main thread to perform the specified task, which can be executed once or repeatedly. TimerTask is an abstract class that implements the Runable interface, representing a task that can be performed by a timer.

Simply try it:

public class Timertasktest extends timertask{private int id;public timertasktest (int id) {this.id = ID;} @Overridepublic void Run () {System.out.println ("Thread:" +id+ "Running ...");}}

public class Timertest {public static void main (string[] args) {Timer ti = new timer (); Ti.schedule (new Timertasktest (1), 5 000);//5 seconds after execution of Ti.schedule (new Timertasktest (2), 1000, 3000);//1 seconds after execution, followed by 3-second cycle execution System.out.println ("Mian end.");}

If you terminate the timer, you can call the timer's Cancle method or call the System.exit method.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.