问题描述
我想在+ -42Mio记录的数据库上运行许多任务。我想分批运行5000个记录/次(结果850个任务)。
我还想限制线程数(到16)java开始为我做这个并且我使用当前代码来完成这个任务:
I want to launch a lot of tasks to run on a database of +-42Mio records. I want to run this in batches of 5000 records/time (results in 850 tasks).I also want to limit the number of threads (to 16) java starts to do this for me and I am using the current code to accomplish this task:
ExecutorService executorService = Executors.newFixedThreadPool(16);
for (int j = 1; j < 900 + 1; j++) {
int start = (j - 1) * 5000;
int stop = (j) * 5000- 1;
FetcherRunner runner = new FetcherRunner(routes, start, stop);
executorService.submit(runner);
Thread t = new Thread(runner);
threadsList.add(t);
t.start();
}
这是正确的方法吗?特别是因为我的印象是java只会触发所有任务......( FetcherRunner
实现 runnable
)
Is this the correct way to do this? Particularly as I have the impression that java just fires away all tasks ...(FetcherRunner
implements runnable
)
推荐答案
使用ExecutorService的第一部分看起来不错:
The first part using ExecutorService looks good:
...
FetcherRunner runner = new FetcherRunner(routes, start, stop);
executorService.submit(runner);
有线程的部分不应该在那里,我假设你在那里只是为了向你展示之前有吗?
The part with Thread should not be there, I am assuming you have it there just to show how you had it before?
更新:
是的,您不需要 executorService之后的代码。提交(跑步者)
,这最终会产生大量的线程。如果您的目标是在循环之后等待所有提交的任务完成,那么您在提交任务时可以获得对 Future
的引用并等待未来
,如下所示:
Update:Yes, you don't require the code after executorService.submit(runner)
, that is going to end up spawning a huge number of threads. If your objective is to wait for all submitted tasks to complete after the loop, then you can get a reference to Future
when submitting tasks and wait on the Future
, something like this:
ExecutorService executorService = Executors.newFixedThreadPool(16);
List<Future<Result>> futures = ..;
for (int j = 1; j < 900+ 1; j++) {
int start = (j - 1) * 5000;
int stop = (j) * 5000- 1;
FetcherRunner runner = new FetcherRunner(routes, start, stop);
futures.add(executorService.submit(runner));
}
for (Future<Result> future:futures){
future.get(); //Do something with the results..
}
这篇关于多线程最佳实践:约束任务newFixedThreadPool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!