Multi-threaded application: Use TimerTask in timer to schedule tasks at any time

Source: Internet
Author: User
Tags getdate
Package com.proj.thread;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Timer;
Import Java.util.TimerTask;  /** * 1, the task in the program after two seconds after the first explosion * 2, after every 3 seconds after the explosion, and then another 5 seconds to perform, so the cycle ... * * * * * * * * * public class Mytimertask extends timertask{private
	
	static int count = 0;                                                @Override public void Run () {++count;
		The number of explosions counter SOP ("the" +count+ "the second explosion, time:" +getdate () + "--bombing");
		if (count%2==1) {new Timer (). Schedule (New Mytimertask (), 3000);
		}else{new Timer (). Schedule (New Mytimertask (), 5000);
		The private static String getDate () {Date date = new Date ();
		SimpleDateFormat SDF = new SimpleDateFormat ("Hh:mm:ss");
		String datestr = Sdf.format (date);
	return datestr;
	protected static void SOP (String string) {System.out.println (string);                  public static void Main (string[] args) {System.out.println (GetDate ());  Start recording time New Timer (). Schedule (New Mytimertask (), 2000);
 First Explosion}}

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.