gpt4 book ai didi

java - ScheduledExecutorService 只调用一个线程

转载 作者:行者123 更新时间:2023-12-01 07:34:05 24 4
gpt4 key购买 nike

我想从队列中提取多个文件并同时解析它们。然而,我的执行器只调用一个线程:

private static ScheduledExecutorService parsingExec ;
protected static BlockingQueue<Path> queue = new LinkedBlockingQueue<>();
int threadPoolSize = 10;
parsingExec = Executors.newScheduledThreadPool(threadPoolSize);
parsingExec.scheduleAtFixedRate(new MyParser(queue), 0, 0, TimeUnit.MILLISECONDS);

最佳答案

来自 scheduleWithFixedRate() 的 Javadoc:

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.

此方法用于调度单个任务,使其执行多次。需要注意的关键一点是,一次只会执行任务的一个实例。如果您想同时执行多个任务,那么您应该使用例如固定线程池。

final ExecutorService parsingExec = Executors.newFixedThreadPool(threadPoolSize);
for(final int i = 0; i < threadPoolSize; i++) {
parsingExec.submit(new MyParser(queue));
}

关于java - ScheduledExecutorService 只调用一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565588/

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