Jam*_*sev 6 java multithreading
以下代码工作正常,但我想知道..从概念上讲,它是否正确?启动线程,等待它们join
.应该ThreadPool
用呢?
如果可能,请发表评论
List<Thread> threads = new ArrayList<Thread>();
for (Test test : testsToBeExecuted) {
Thread t = new Thread(test);
threads.add(t);
t.start();
}
for (Thread thread : threads) {
thread.join();
}
Run Code Online (Sandbox Code Playgroud)
从概念上讲,它看起来很好 您可以使用您创建的ExecutorService,例如:
ExecutorService service = Executors.newFixedThreadPool(testsToBeExecuted.size());
Run Code Online (Sandbox Code Playgroud)
Thenyou会在执行程序服务本身上创建一个Callables列表和invokeAll.这本质上也会做同样的事情.
归档时间: |
|
查看次数: |
197 次 |
最近记录: |