简体   繁体   English

为什么ScheduledExecutorService无法按需生成线程?

[英]Why doesn't ScheduledExecutorService spawn threads as needed?

In my application I use ScheduledExecutorService, but only one thread is spawned to handle the scheduled tasks. 在我的应用程序中,我使用ScheduledExecutorService,但仅产生一个线程来处理计划的任务。 Is this because ScheduledExecutorService does not spawn threads to handle the pending tasks? 这是因为ScheduledExecutorService不会生成线程来处理待处理的任务吗?

Here is a code snippet that will output only "run() 1" instead of the expected "run() 1" followed by "run() 2" ... "run() 10." 这是一个代码片段,将仅输出“ run()1”,而不是预期的“ run()1”,后跟“ run()2” ...“ run()10”。

public class App {

    public static void main(String[] args) {
        int N = 10;
        Runnable runner = new Runnable() {

            public void run() {
                foo();
            }
        };
        for (int i = 0; i < N; i++) {
            executor.schedule(runner, i, TimeUnit.MILLISECONDS);
        }
    }

    private static void foo() {
        System.out.println("run() " + (++n));
        synchronized (executor) {
            try {
                executor.wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println("finished()");
    }
    private static Logger logger = Logger.getLogger(App.class.getName());
    private static int n = 0;
    private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
}

There is only one thread because you create the thread pool with Executors.newScheduledThreadPool(1) , which means that the thread pool contains only 1 thread. 只有一个线程,因为您使用Executors.newScheduledThreadPool(1)创建了线程池,这意味着线程池仅包含1个线程。 If you want 10 threads, pass 10 as argument. 如果需要10个线程,则将10作为参数传递。 Note that the documentation of ScheduledThreadPoolExecutor , which is what this method returns, explicitly says that the thread pool has a fixed size. 请注意,此方法返回的ScheduledThreadPoolExecutor文档明确指出线程池的大小是固定的。

From the javadoc for ScheduledThreadPoolExecutor : ScheduledThreadPoolExecutor的javadoc中:

While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. 尽管此类从ThreadPoolExecutor继承,但是一些继承的调整方法对此没有用。 In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. 特别是,由于它使用corePoolSize线程和无限制队列充当固定大小的池,因此对maximumPoolSize的调整没有任何作用。 Additionally, it is almost never a good idea to set corePoolSize to zero or use allowCoreThreadTimeOut because this may leave the pool without threads to handle tasks once they become eligible to run. 另外,将corePoolSize设置为零或使用allowCoreThreadTimeOut几乎不是一个好主意,因为一旦有资格运行任务,这可能会使池中没有线程来处理任务。

In other words, maximumPoolSize == corePoolSize . 换句话说, maximumPoolSize == corePoolSize You set corePoolSize to 1 , so that's all it will spawn. 您将corePoolSize设置为1 ,这样就可以生成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM