Java Timer: Timer

Source: Internet
Author: User

1. Schedule(timertask task, date time): Starts execution of the Run method in TimerTask on the specified date date
2. Scheduledelay): After a period of delay, start executing the Run method in TimerTask
3. Scheduledelay, long period): After a period of delay, start executing the Run method in TimerTask, and repeat the Run method after every period seconds



1 and 2 Examples:
Package Com.timer;import Java.util.date;import Java.util.timer;import Java.util.timertask;public class Run1 {private Static Timer timer = new timer ();/* TimerTask implementation class, and override the Run () method */static public class MyTask extends timertask{@Overridepu Blic void Run () {System.out.println ("ran! Time is: "+ new Date ());}} public static void Main (string[] args) {try{mytask task = new MyTask ();//timer.schedule (task, 5000);//After 5 seconds delay, Start executing the Run () method in the Task Timer.schedule (task, 5000, 2000);//After 5 seconds of delay, start executing the Run () method in the task and repeat the run () method every two seconds later}catch (Exception E ) {e.printstacktrace ();}}}

  

3 Examples:

Package Com.timer;import Java.text.simpledateformat;import Java.util.date;import java.util.timer;import Java.util.timertask;public class Run1 {private static timer timer = new timer (),/* * TimerTask implementation class, and override the Run () method */static Pu Blic class MyTask extends timertask{@Overridepublic void Run () {System.out.println ("ran! Time is: "+ new Date ());}} public static void Main (string[] args) {try{mytask task = new MyTask (); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String datestring = "2017-10-17 9:45:00";D ate dateref = Sdf.parse (datestring); System.out.println ("String time:" + dateref.tolocalestring () + "  Current time:" + new Date (). toLocaleString ()); Timer.schedule ( task, dateref);//start executing the Run () method on the specified date, the specified date is later than your current system time}catch (Exception e) {e.printstacktrace ();}}}

Run effect

String time:£ º9:  Current time:10 -9:: £ º xx  .

Java Timer: Timer

Related Article

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.