Description: First on the code, note follow-up supplement.
public class ScheduleTest1 {
private static long start;
private static Scheduledexecutorservice Executorservice = Executors.newscheduledthreadpool (2);
/**
* If the output after Timer.schedule plus sleep is 1001 6005
* The sleep of the first thread affects the second thread
* If the first thread is dead, the Gothic thread will not execute
*timer is associated with system time.
*
* The above problems will not occur with Executorservice
*
* @param args
*/
public static void Main (string[] args) {
TimerTask task = new TimerTask () {
@Override
public void Run () {
System.out.println (System.currenttimemillis ()-start);
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
// }
throw new RuntimeException ();
}
};
TimerTask Task1 = new TimerTask () {
@Override
public void Run () {
System.out.println (System.currenttimemillis ()-start);
}
};
Timer timer = new timer ();
Start = System.currenttimemillis ();
1s after execution
Timer.schedule (task,1000);
3s after execution
Timer.schedule (task1,3000);
Executorservice.schedule (task,1000, timeunit.milliseconds);
Executorservice.schedule (task1,3000, timeunit.milliseconds);
}
}
Java Concurrent Programming 4