粗大事了~~~~兄弟们,ScheduledExecutorService的scheduleAtFixedRate方法怎么会这样???????

最勇敢的鸟 2013-11-28 10:40:26

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秒起一个,居然要等待第一个线程结束才起下一个。
不信都考到自己电脑试一试

...全文
1520 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
oh_Maxy 2013-11-28
  • 打赏
  • 举报
回复
引用 6 楼 sunli880127 的回复:
[quote=引用 5 楼 Inhibitory 的回复:] 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 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.
I LOVE YOU 看中文DOC是我最大的错[/quote] scheduleAtFixedRate ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。如果任务的任何一个执行遇到异常,则后续执行都会被取消。否则,只能通过执行程序的取消或终止方法来终止该任务。如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行。
oh_Maxy 2013-11-28
  • 打赏
  • 举报
回复
引用 3 楼 sunli880127 的回复:
[quote=引用 2 楼 oh_Maxy 的回复:] 这是定时任务,就是定期执行的。 而且,你这里就一个new Cat()线程吧? ThreadPoolExecutor应该能满足你多线程的实验。
这个线程池框架的作用是每2秒启动一个这样的线程,不止一个的[/quote] new 操作只有一次,定期执行,都是同一个线程的执行。LZ可以跟代码看看,或者反过来想想:我自己随便new了一个Runnable的实现类,定时任务管理器怎么知道我的内部构造,如何再new一个新的呢?
最勇敢的鸟 2013-11-28
  • 打赏
  • 举报
回复
引用 5 楼 Inhibitory 的回复:
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 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.
I LOVE YOU 看中文DOC是我最大的错
Inhibitory 2013-11-28
  • 打赏
  • 举报
回复
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 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.
最勇敢的鸟 2013-11-28
  • 打赏
  • 举报
回复
引用 1 楼 etnet 的回复:
好好看看JavaDOC吧.....
原谅我使用中文的DOC scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。 看到了吧?就应该是每两秒执行一次,不知你有何高见?
最勇敢的鸟 2013-11-28
  • 打赏
  • 举报
回复
引用 2 楼 oh_Maxy 的回复:
这是定时任务,就是定期执行的。 而且,你这里就一个new Cat()线程吧? ThreadPoolExecutor应该能满足你多线程的实验。
这个线程池框架的作用是每2秒启动一个这样的线程,不止一个的
oh_Maxy 2013-11-28
  • 打赏
  • 举报
回复
这是定时任务,就是定期执行的。 而且,你这里就一个new Cat()线程吧? ThreadPoolExecutor应该能满足你多线程的实验。
etnet 2013-11-28
  • 打赏
  • 举报
回复
好好看看JavaDOC吧.....
最勇敢的鸟 2013-11-28
  • 打赏
  • 举报
回复
引用 8 楼 oh_Maxy 的回复:
[quote=引用 6 楼 sunli880127 的回复:] [quote=引用 5 楼 Inhibitory 的回复:] 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 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.
I LOVE YOU 看中文DOC是我最大的错[/quote] scheduleAtFixedRate ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。如果任务的任何一个执行遇到异常,则后续执行都会被取消。否则,只能通过执行程序的取消或终止方法来终止该任务。如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行。[/quote] 好吧,看来是我看的不仔细,因为没看下面的详情

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧