日期:2014-05-18  浏览次数:20619 次

粗大事了~~~~兄弟们,ScheduledExecutorService的scheduleAtFixedRate方法怎么会这样???????
本帖最后由 sunli880127 于 2013-11-28 10:54:12 编辑

public class TestSchedulerThreadPool {

    public static void main(String[] args) {

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
        executorService.scheduleAtFixedRate(new Cat(),1,2, TimeUnit.SECONDS);

    }

}

class Cat implements Runnable{

    @Override
    public void run() {
        System.out.println("我起来了");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            System.out.println("shit");
        }
        System.out.println("我回去了");
    }
}

这玩意的执行结果是:
我起来了
我回去了
我起来了
我回去了
我起来了
我回去了

这个感觉肿么像是Executors.newSingleThreadScheduledExecutor()单线程执行框架的结果呢?
明明应该是每两秒起一个线程的,现在非要5秒起一个,居然要等待第一个线程结束才起下一个。
不信都考到自己电脑试一试

ScheduledExecutor JAVA 定时任务 线程池框架

------解决方案--------------------
好好看看JavaDOC吧.....
------解决方案--------------------
这是定时任务,就是定期执行的。
而且,你这里就一个new Cat()线程吧?

ThreadPoolExecutor应该能满足你多线程的实验。
------解决方案--------------------
ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                     long initialDelay,
                                     long period,
                                     TimeUnit unit)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