-
{
-
@Override
-
public void run()
-
{
-
System.out.println("task2 invoked ! "
-
+ (System.currentTimeMillis() - start));
-
}
-
};
-
Timer timer = new Timer();
-
start = System.currentTimeMillis();
-
timer.schedule(task1, 1000);
-
timer.schedule(task2, 3000);
-
}
-
}
定义了两个任务,预计是第一个任务1s后执行,第二个任务3s后执行,但是看运行结果:
[java] view plain copy
-
task1 invoked ! 1000
-
task2 invoked ! 4000
task2实际上是4后才执行,正因为Timer内部是一个线程,而任务1所需的时间超过了两个任务间的间隔导致。下面使用ScheduledThreadPool解决这个问题:
[java] view plain copy
-
package com.zhy.concurrency.timer;
-
import java.util.TimerTask;
-
import java.util.concurrent.Executors;
-
import java.util.concurrent.ScheduledExecutorService;
-
import java.util.concurrent.TimeUnit;
-
public class ScheduledThreadPoolExecutorTest
-
{
-
private static long start;
-
public static void main(String[] args)
-
{
-
/**
-
* 使用工厂方法初始化一个ScheduledThreadPool
-
*/
-
ScheduledExecutorService newScheduledThreadPool = Executors
-
.newScheduledThreadPool(2);
-
TimerTask task1 = new TimerTask()
-
{
-
@Override
-
public void run()
-
{
-
try
-
{
-
System.out.println("task1 invoked ! "
-
+ (System.currentTimeMillis() - start));
-
Thread.sleep(3000);
-
} catch (Exception e)
-
{
-
e.printStackTrace();
-
}
-
}
-
};
-
TimerTask task2 = new TimerTask()
-
{
-
@Override
-
public void run()
-
{
-
System.out.println("task2 invoked ! "
-
+ (System.currentTimeMillis() - start));
-
}
-
};
-
start = System.currentTimeMillis();
-
newScheduledThreadPool.schedule(task1, 1000, TimeUnit.MILLISECONDS);
-
newScheduledThreadPool.schedule(task2, 3000, TimeUnit.MILLISECONDS);
-
}
-
}
输出结果:
[java] view plain copy
-
task1 invoked ! 1001
-
task2 invoked ! 3001
符合我们的预期结果。因为ScheduledThreadPool内部是个线程池,所以可以支持多个任务并发执行。
2、Timer当任务抛出异常时的缺陷
如果TimerTask抛出RuntimeException,Timer会停止所有任务的运行:
[java] view plain copy
-
package com.zhy.concurrency.timer;
-
import java.util.Date;
-
import java.util.Timer;
-
import java.util.TimerTask;
-
public class ScheduledThreadPoolDemo01
-
{
-
public static void main(String[] args) throws InterruptedException
-
{
-
final TimerTask task1 = new TimerTask()
-
{
-
@Override
-
public void run()
-
{
-
throw new RuntimeException();
-
}
-
};
-
final TimerTask task2 = new TimerTask()
-
{
-
@Override
-
public void run()
-
{
-
System.out.println(“task2 invoked!”);
-
}
-
};
-
Timer timer = new Timer();
-
timer.schedule(task1, 100);
-
timer.scheduleAtFixedRate(task2, new Date(), 1000);
-
}
-
}
上面有两个任务,任务1抛出一个运行时的异常,任务2周期性的执行某个操作,输出结果:
[java] view plain copy
-
task2 invoked!
-
Exception in thread “Timer-0” java.lang.RuntimeException
-
at com.zhy.concurrency.timer.ScheduledThreadPoolDemo01$1.run(ScheduledThreadPoolDemo01.java:24)
-
at java.util.TimerThread.mainLoop(Timer.java:512)
-
at java.util.TimerThread.run(Timer.java:462)
由于任务1的一次,任务2也停止运行了。。。下面使用ScheduledExecutorService解决这个问题:
[java] view plain copy
-
package com.zhy.concurrency.timer;
-
import java.util.Date;
-
import java.util.Timer;
-
import java.util.TimerTask;
-
import java.util.concurrent.Executors;
-
import java.util.concurrent.ScheduledExecutorService;
-
import java.util.concurrent.TimeUnit;
-
public class ScheduledThreadPoolDemo01
-
{
-
public static void main(String[] args) throws InterruptedException
-
{
-
final TimerTask task1 = new TimerTask()
-
{
-
@Override
-
public void run()
-
{
-
throw new RuntimeException();
-
}
-
};
-
final TimerTask task2 = new TimerTask()
-
{
-
@Override
-
public void run()
-
{
-
System.out.println(“task2 invoked!”);
-
}
-
};
-
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
-
pool.schedule(task1, 100, TimeUnit.MILLISECONDS);