Java executor中等效的Thread.join()
我有个新手问题。我有以下代码:Java executor中等效的Thread.join(),java,multithreading,executorservice,Java,Multithreading,Executorservice,我有个新手问题。我有以下代码: public class Main { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub IntHolder aHolder=new IntHolder(); aHolder.Number=0; IncrementorTh
public class Main
{
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated method stub
IntHolder aHolder=new IntHolder();
aHolder.Number=0;
IncrementorThread A= new IncrementorThread(1, aHolder);
IncrementorThread B= new IncrementorThread(2, aHolder);
IncrementorThread C= new IncrementorThread(3, aHolder);
A.start();
B.start();
C.start();
A.join();
B.join();
C.join();
System.out.println("All threads completed...");
}
}
这将等待所有线程完成。如果我像这样使用执行器
:
public class Main
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
IntHolder aHolder=new IntHolder();
aHolder.number=0;
IncrementalRunable A= new IncrementalRunable(1, aHolder);
IncrementalRunable B= new IncrementalRunable(2, aHolder);
IncrementalRunable C= new IncrementalRunable(3, aHolder);
ExecutorService exec = Executors.newFixedThreadPool(3);
exec.execute(A);
exec.execute(B);
exec.execute(C);
//Don't know what to do here
System.out.println("All threads completed...");
}
}
如何挂起主线程以等待执行器中的所有线程完成,即在所有线程完成其工作后应打印“所有线程已完成…”
exec.shutdown();
// Wait until all threads are finish
exec.awaitTermination();
使用shutdown()
编辑:
基于@Lital的评论
List<Callable<Object>> calls = new ArrayList<Callable<Object>>();
calls.add(Executors.callable(new IncrementalRunable(1, aHolder)));
calls.add(Executors.callable(new IncrementalRunable(2, aHolder)));
calls.add(Executors.callable(new IncrementalRunable(3, aHolder)));
List<Future<Object>> futures = executor.invokeAll(calls);
List calls=newarraylist();
calls.add(Executors.callable(newincrementalrunable(1,aHolder));
calls.add(Executors.callable(newincrementalrunable(2,aHolder));
calls.add(Executors.callable(newincrementalrunable(3,aHolder));
List futures=executor.invokeAll(调用);
注:
invokeAll()
将在所有任务完成之前(通过执行失败或成功完成)不会返回。如果要等待任务完成,则不应像这样使用executor。
如果您不想/无法关闭线程池执行器,该怎么办?
这是一种更推荐的方式:
ExecutorService exec = Executors.newFixedThreadPool(3);
Collection<Future<?>> tasks = new LinkedList<Future<?>>();
Future<T> future = exec.submit(A);
tasks.add(future);
future = exec.submit(B);
tasks.add(future);
future = exec.submit(C);
tasks.add(future);
// wait for tasks completion
for (Future<?> currTask : tasks) {
try {
currTask.get();
} catch (Throwable thrown) {
Logger.error(thrown, "Error while waiting for thread completion");
}
}
ExecutorService exec=Executors.newFixedThreadPool(3);
收藏>();
未来=执行提交(A);
任务。添加(未来);
未来=执行提交(B);
任务。添加(未来);
未来=执行提交(C);
任务。添加(未来);
//等待任务完成
用于(未来任务:任务){
试一试{
currTask.get();
}接球(可抛投){
error(抛出“等待线程完成时出错”);
}
}
尝试以这种方式使用线程池
executor.shutdown();
executor.awaitTermination(MAX_PRIORITY, TimeUnit.HOURS);
我们可以使用下面的代码来连接线程
executor.execute(new YouThread());
try{
executor.shutdown();
while (!executor.awaitTermination(24L, TimeUnit.HOURS)) {
System.out.println("Not yet. Still waiting for termination");
}
}catch(InterruptedException e){
e.printStackTrace();
}
检查此项:Future有一种阻塞获取方法。你不认为仅仅为了简单的连接而关闭线程池不是一种好的做法吗?@Lital我添加了另一种可以遵循的巧妙方法。@NarendraPathai,太好了,这甚至更好,但将数字改为1,2,3(这些是我用来打印消息的线程编号)顺便说一句:没有必要等待终止,除非你需要对此做出反应。因此,如果您之后所做的只是结束程序,那么只要知道线程将正确终止,一个简单的shutdown()
调用就足够了。否则,作为对shutdown()
@mariotilov Done的反应,需要一个强有力的shutdownNow()
,一个输入错误:)我同意我不应该使用这样的执行器,但我现在只是在玩弄java,我对如何实现这一点很好奇
executor.execute(new YouThread());
try{
executor.shutdown();
while (!executor.awaitTermination(24L, TimeUnit.HOURS)) {
System.out.println("Not yet. Still waiting for termination");
}
}catch(InterruptedException e){
e.printStackTrace();
}