-------
android培训、
java培训、期待与您交流! ----------
------- android培训、 java培训、期待与您交流! ----------
1、Timer
包:java.util.Timer
构造方法:
Timer() //创建一个新计时器
Timer(boolean isDaemon) //创建一个新计时器,可以指定其相关的线程作为守护程序运行
Timer(String name) //创建一个新计时器,其相关的线程具有指定的名称
Timer(String name, boolean isDaemon) //创建一个新计时器,其相关的线程具有指定的名称,并且可以指定作为守护程序运行
方法摘要:
schedule(TimerTask task, Date time) //安排在指定的时间执行指定的任务 ,TimerTaske implements Runnable
schedule(TimerTask task, Date firstTime, long period)//安排指定的任务在指定的时间开始进行重复的固定延迟执行
schedule(TimerTask task, long delay, long period) // 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)//安排指定的任务在指定的时间开始进行重复的固定速率执行
scheduleAtFixedRate(TimerTask task, long delay, long period)//安排指定的任务在指定的延迟后开始进行重复的固定速率执行
2、ScheduledThreadPoolExecutor 引入
继承关系:
java.lang.Object
java.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ScheduledThreadPoolExecutor
public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService
方法摘要:
ScheduledFuture schedule(Callable<V> callable, long delay, TimeUnit unit) //在指定的延迟后执行
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)//在指定的延迟后执行
ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)//在指定的延迟后以固定速率执行(类似Timer.scheduleAtFixedRate())
ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)//在指定的延迟后以固定间隔执行(类似Timer.schedule())
3、ScheduledThreadPoolExecutor 与Timer比较:
(1)Timer对调度的支持是基于绝对时间的,因此任务对系统时间的改变是敏感的;而ScheduledThreadPoolExecutor支持相对时间。
(2)Timer使用单线程方式来执行所有的TimerTask,如果某个TimerTask很耗时则会影响到其他TimerTask的执行;而ScheduledThreadPoolExecutor则可以构造一个固定大小的线程池来执行任务。
(3)Timer 不会捕获由TimerTask抛出的未检查异常,故当有异常抛出时,Timer会终止,导致未执行完的TimerTask不再执行,新的 TimerTask也不能被调度;ScheduledThreadPoolExecutor对这个问题进行了妥善的处理,不会影响其他任务的执行。
4、实例
public class ScheduledThreadPoolExec{
public static void main( String[] args) throws InterruptedException, ExecutionException{
//线程池只有一个线程,所有的任务都由这个线程来执行和调度
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
System.out.println("任务执行之前 : "+new Date());
final ScheduledFuture future1 = executor.schedule(new Runnable() {
public void run() {
System.out.println(" 任务执行之后,future1 : "+Thread.currentThread().getName()+" : "+new Date());
}
}, 5, TimeUnit.SECONDS);
final ScheduledFuture future2 = executor.schedule(new Runnable() {
public void run() {
System.out.println("任务执行之后,future2 : "+Thread.currentThread().getName()+" : "+new Date());
}
}, 3, TimeUnit.SECONDS);
// 执行抛出异常的任务
executor.schedule(new ExceptionTask(), 1, TimeUnit.SECONDS);
executor.schedule(new ExceptionTask(), 2, TimeUnit.SECONDS);
final BlockingQueue blockingQueue = new ArrayBlockingQueue(2, true);
blockingQueue.add(future2);
blockingQueue.add(future1);
System.out.println(new Date());
//等待任务的执行完毕
while (!blockingQueue.isEmpty()){
final ScheduledFuture future = (ScheduledFuture) blockingQueue.poll();
if (!future.isDone()){
blockingQueue.add(future);
}else{
System.out.println(future.get());
}
}
System.out.println(new Date());
executor.shutdown();
}
}
class ExceptionTask extends TimerTask{
public void run(){
System.out.println("TimerExceptionTask: "+Thread.currentThread().getName() +" : "+ new Date());
throw new RuntimeException();
}
}
实例测试证实,线程池里面的线程在执行任务的过程中如果发生异常,并不会终止后面其他任务的执行。
------- android培训、 java培训、期待与您交流! ----------