Description: Code first, and notes will be added later.
public class ScheduleTest1 {
private static long start;
private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
/**
* If timer.schedule plus sleep, the output is 1001 6005
* The sleep of the first thread will affect the second
* If the first thread hangs up, the next thread will not execute *
timer is related to the system time
*
* The above problems will not occur when using 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后执行
// timer.schedule(task,1000);
// //3s后执行
// timer.schedule(task1,3000);
executorService.schedule(task,1000, TimeUnit.MILLISECONDS);
executorService.schedule(task1,3000, TimeUnit.MILLISECONDS);
}
}
JAVA concurrent programming 4
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324684518&siteId=291194637
Ranking