gpt4 book ai didi

java - 使用 newSingleThreadScheduledExecutor 调度多个任务

转载 作者:行者123 更新时间:2023-12-03 12:45:43 26 4
gpt4 key购买 nike

如果我为 ScheduledExecutorService 安排了多个任务,但我已经确定只能有一个线程来执行所有任务,每个任务会按顺序执行吗?

例如:在下面的场景中会发生什么

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleWithFixedDelay(new Runnable{void run(){....}}, 6000,6000,TimeUnit.MILLISECONDS);
scheduler.scheduleWithFixedDelay(new Runnable{void run(){....}}, 6000,6000,TimeUnit.MILLISECONDS);

最佳答案

来自javadoc:

Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

http://download.java.net/jdk7/archive/b123/docs/api/java/util/concurrent/Executors.html#newSingleThreadExecutor(java.util.concurrent.ThreadFactory)

关于java - 使用 newSingleThreadScheduledExecutor 调度多个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17603844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com