日期:2014-05-20  浏览次数:20674 次

关于ScheduledThreadPoolExecutor的一点疑惑
Java code


    ScheduledThreadPoolExecutor pe=new ScheduledThreadPoolExecutor(2);
    Task task = new Task();
    pe.scheduleAtFixedRate(task,0, 10, TimeUnit.SECONDS);
Task类
public class Task implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("Task:" + Thread.currentThread());
            System.out.println("发送完毕");
            throw new Exception();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


为什么执行的时候遇到异常没有停止还是在执行呢 
API里面是这样说的 
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

小弟的理解就是说这个方法里面的一次遇到了异常就不会在周期性执行了,但是结果却还是执行,请大神们指点!!!!

------解决方案--------------------
你把异常都try catch啦,调度程序当然不知道出错啦。