执行器中的Thread.join()等价物 [英] Thread.join() equivalent in executor

查看:115
本文介绍了执行器中的Thread.join()等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新手问题。我有这个代码:

  public class Main 
{

public static void main( String [] args)抛出InterruptedException
{
// TODO自动生成的方法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(所有线程已完成......);

}

}

哪会等为所有线程完成。如果我像这样使用执行者

  public class Main 
{

public static void main(String [] args)
{
// TODO自动生成的方法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);
//不知道该做什么

System.out.println(所有线程已完成...);
}
}

如何暂停主线程等待所有执行程序中的线程完成,即所有线程完成...应该在所有线程完成工作后打印出来?

解决方案

  executor.shutdown(); 
while(!executor.awaitTermination(24L,TimeUnit.HOURS)){
System.out.println(还没有。还在等待终止);
}

使用 shutdown()+ awaitTermination()组合。



编辑:



基于@Lital的评论

 列表< 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)));

列表< Future< Object>> futures = executor.invokeAll(calls);

注意:
invokeAll()在完成所有任务之前(通过失败或完成成功执行)将不会返回。


I have a newbie question. I have this code:

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...");

    }

}

Which will wait for all threads to complete. If I use Executors like this:

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...");
    }
}

How can I suspend the main thread to wait for all the threads in the executor to finish, i.e the "All threads completed..." should be printed after the all the threads have done their work?

解决方案

executor.shutdown();
while (!executor.awaitTermination(24L, TimeUnit.HOURS)) {
    System.out.println("Not yet. Still waiting for termination");
}

Use shutdown() + awaitTermination() combination.

EDIT:

Based on the comment of @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);

NOTE: invokeAll() will not return until all the tasks are completed (either by failing or completing successful execution).

这篇关于执行器中的Thread.join()等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆