Created
July 21, 2012 02:29
-
-
Save ucheng/3154270 to your computer and use it in GitHub Desktop.
Timer schedule short execution time task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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