在上一篇https://blog.csdn.net/hujiaxi222/article/details/85274335简单介绍Timer的使用后发现Timer在多线程中使用会有问题
Timer timer = new Timer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("schedule--0 * 1000-A");
}
}, 0, 5 * 1000);
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("schedule--10 * 1000-B");
System.out.println(sdf.format(new Date()) + " B: sleep");
try {
Thread.sleep(20*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}