【发布时间】:2022-11-22 09:21:54
【问题描述】:
我试图让一个线程加载记录(比如从数据库)。该线程将记录馈送到处理这些单独任务的线程池中。
我原以为这段代码可以工作,但它打印数字直到 60,然后停止。
ThreadPoolTaskExecutor accountLoaderTaskExecutor = new ThreadPoolTaskExecutor();
accountLoaderTaskExecutor.setCorePoolSize(1);
accountLoaderTaskExecutor.setMaxPoolSize(1);
accountLoaderTaskExecutor.initialize();
ThreadPoolTaskExecutor accountDeletionTaskExecutor = new ThreadPoolTaskExecutor();
accountDeletionTaskExecutor.setCorePoolSize(10);
accountDeletionTaskExecutor.setMaxPoolSize(10);
accountDeletionTaskExecutor.setQueueCapacity(50);
accountDeletionTaskExecutor.initialize();
accountLoaderTaskExecutor.submit(() -> {
List<Integer> customerAccountIds = getCustomerAccountIds(); // return 1000s integers
customerAccountIds.forEach(id -> {
accountDeletionTaskExecutor.submit(() -> {
try {
System.out.println(id);
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
});
});
Thread.currentThread().join();
我原以为 accountLoaderTaskExecutor 线程会在 accountDeletionTaskExecutor.submit 上阻塞,但随后继续处理记录,直到耗尽所有 customerAccountId。
【问题讨论】:
标签: java multithreading concurrency