Skip to content

Instantly share code, notes, and snippets.

@ucheng
Created July 21, 2012 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ucheng/3154270 to your computer and use it in GitHub Desktop.
Save ucheng/3154270 to your computer and use it in GitHub Desktop.
Timer schedule short execution time task
package com.stanley;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class ShortExecutionTimeTask {
public static void main(String[] args) {
Timer timer = new Timer();
ShowCurrentTimeTask task = new ShowCurrentTimeTask();
timer.schedule(task, new Date(), 3000);
}
static class ShowCurrentTimeTask extends TimerTask {
int count = 1;
@Override
public void run() {
System.out.println("===>task execution count: " + count++);
System.out.println("task schedule time: " + new Date(this.scheduledExecutionTime()));
System.out.println("task execution time: " + new Date());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("oops, something goes wrong");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment