Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
线程使用java等待所有线程返回输出?_Java_Multithreading - Fatal编程技术网

线程使用java等待所有线程返回输出?

线程使用java等待所有线程返回输出?,java,multithreading,Java,Multithreading,可能重复: 我想运行三个线程,每个线程调用不同的类。这些类执行一些处理并返回一个Boolean值,我想等待所有三个线程返回它们的输出。我想知道如何使用Java实现这一点 谢谢您可以使用Thread.join()来执行此操作: Thread[] threads = new Thread[numOfThreads]; for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(new Runnable() {

可能重复:

我想运行三个线程,每个线程调用不同的类。这些类执行一些处理并返回一个
Boolean
值,我想等待所有三个线程返回它们的输出。我想知道如何使用Java实现这一点


谢谢

您可以使用
Thread.join()
来执行此操作:

Thread[] threads = new Thread[numOfThreads];
for (int i = 0; i < threads.length; i++) {
    threads[i] = new Thread(new Runnable() {
        public void run() {
            System.out.println("xxx");
        }
    });
    threads[i].start();
}

for (int i = 0; i < threads.length; i++) {
    try {
        threads[i].join();
    } catch (InterruptedException e) {
    }
}
Thread[]threads=新线程[numOfThreads];
对于(int i=0;i
为您的解决方案

Thread[] threads = new Thread[3];
threads[i] = new Thread(new Runnable() {
        ...
}).start();
threads[i] = new Thread(new Runnable() {
        ...
}).start();
threads[i] = new Thread(new Runnable() {
        ...
}).start();

for (int i = 0; i < threads.length; i++) {
    try {
        threads[i].join();
    } catch (InterruptedException e) {
    }
}
Thread[]threads=新线程[3];
threads[i]=新线程(new Runnable(){
...
}).start();
threads[i]=新线程(new Runnable(){
...
}).start();
threads[i]=新线程(new Runnable(){
...
}).start();
对于(int i=0;i
您可以使用
Thread.join()
执行此操作:

Thread[] threads = new Thread[numOfThreads];
for (int i = 0; i < threads.length; i++) {
    threads[i] = new Thread(new Runnable() {
        public void run() {
            System.out.println("xxx");
        }
    });
    threads[i].start();
}

for (int i = 0; i < threads.length; i++) {
    try {
        threads[i].join();
    } catch (InterruptedException e) {
    }
}
Thread[]threads=新线程[numOfThreads];
对于(int i=0;i
为您的解决方案

Thread[] threads = new Thread[3];
threads[i] = new Thread(new Runnable() {
        ...
}).start();
threads[i] = new Thread(new Runnable() {
        ...
}).start();
threads[i] = new Thread(new Runnable() {
        ...
}).start();

for (int i = 0; i < threads.length; i++) {
    try {
        threads[i].join();
    } catch (InterruptedException e) {
    }
}
Thread[]threads=新线程[3];
threads[i]=新线程(new Runnable(){
...
}).start();
threads[i]=新线程(new Runnable(){
...
}).start();
threads[i]=新线程(new Runnable(){
...
}).start();
对于(int i=0;i
线程有一个
连接方法。如果主线程在线程
t
上调用该方法,则主线程将等待
t
完成


请参见()

线程有一个
join
方法。如果主线程在线程
t
上调用该方法,则主线程将等待
t
完成

请参阅()

您可以使用“屏障”(有关Java API中的实现,请参阅)。或者,如果您只想获得输出,但不需要在处理输出之前完成所有计算,则可以使用在获得结果时需要阻塞的函数来表示计算。

您可以使用“屏障”(有关Java API中的实现,请参阅)。或者,如果您只想获得输出,但不需要在处理输出之前完成所有计算,则可以在获得结果时使用Thread.join()表示您的计算,如果需要,它将阻止计算。

您可以使用Thread.join()如下所示:-

Thread t1 = new Thread(myRunnable1);
Thread t2 = new Thread(myRunnable2);
Thread t3 = new Thread(myRunnable3);

t1.start();
t2.start();
t3.start();

t1.join();
t2.join();
t3.join();

//do something when all 3 threads finish
我希望这有助于达到你的要求

问候,

Charlee Ch.

您可以按如下方式使用Thread.join():-

Thread t1 = new Thread(myRunnable1);
Thread t2 = new Thread(myRunnable2);
Thread t3 = new Thread(myRunnable3);

t1.start();
t2.start();
t3.start();

t1.join();
t2.join();
t3.join();

//do something when all 3 threads finish
我希望这有助于达到你的要求

问候,


Charlee Ch.

如果您使用的是Executor服务,您可以

 ExecutorService es = /* create a reusable thread pool */

 List<Future> futures = new ArrayList<Future>();
 futures.add(es.submit(myRunnable1));
 futures.add(es.submit(myRunnable2));
 futures.add(es.submit(myRunnable3));
 for(Future f: futures) f.get(); // wait for them to finish.
executors=/*创建一个可重用的线程池*/
列表期货=新的ArrayList();
futures.add(es.submit(myRunnable1));
futures.add(es.submit(myRunnable2));
futures.add(es.submit(myRunnable3));
for(Future f:futures)f.get();//等待他们完成。
如果要返回布尔值,则应使用可调用的。您还可以使用invokeAll

 ExecutorService es = /* create a reusable thread pool */

 List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
 es.invokeAll(Arrays.asList(
    new Callable<Boolean>() {
        public Boolean call() {
             return true;
        }
    },
    new Callable<Boolean>() {
        public Boolean call() {
             return false;
        }
    },
    new Callable<Boolean>() {
        public Boolean call() {
             return true;
        }
    }
 ));

 for(Future<Boolean> f: futures) {
      Boolean result = f.get();
 }
executors=/*创建一个可重用的线程池*/
列表期货=新的ArrayList();
es.invokeAll(Arrays.asList(
新的可调用(){
公共布尔调用(){
返回true;
}
},
新的可调用(){
公共布尔调用(){
返回false;
}
},
新的可调用(){
公共布尔调用(){
返回true;
}
}
));
for(未来f:未来){
布尔结果=f.get();
}

如果您使用的是Executor服务,您可以

 ExecutorService es = /* create a reusable thread pool */

 List<Future> futures = new ArrayList<Future>();
 futures.add(es.submit(myRunnable1));
 futures.add(es.submit(myRunnable2));
 futures.add(es.submit(myRunnable3));
 for(Future f: futures) f.get(); // wait for them to finish.
executors=/*创建一个可重用的线程池*/
列表期货=新的ArrayList();
futures.add(es.submit(myRunnable1));
futures.add(es.submit(myRunnable2));
futures.add(es.submit(myRunnable3));
for(Future f:futures)f.get();//等待他们完成。
如果要返回布尔值,则应使用可调用的。您还可以使用invokeAll

 ExecutorService es = /* create a reusable thread pool */

 List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
 es.invokeAll(Arrays.asList(
    new Callable<Boolean>() {
        public Boolean call() {
             return true;
        }
    },
    new Callable<Boolean>() {
        public Boolean call() {
             return false;
        }
    },
    new Callable<Boolean>() {
        public Boolean call() {
             return true;
        }
    }
 ));

 for(Future<Boolean> f: futures) {
      Boolean result = f.get();
 }
executors=/*创建一个可重用的线程池*/
列表期货=新的ArrayList();
es.invokeAll(Arrays.asList(
新的可调用(){
公共布尔调用(){
返回true;
}
},
新的可调用(){
公共布尔调用(){
返回false;
}
},
新的可调用(){
公共布尔调用(){
返回true;
}
}
));
for(未来f:未来){
布尔结果=f.get();
}

请看中的示例请看
ExecutorService
的+1中的示例,但是
invokeAll
可能是一个更好的选择。我想到了这一点,但invokeAll需要一个可调用的集合实际上,一个Callable是可以被调用的(双关语),因为他希望它们返回一个布尔值。我支持invokeAll()的使用。总是有Executors.callable()包装一个Runnable。@Qwerty,sjlee,我添加了一个使用callable和invokeAll的示例。+1表示
ExecutorService
,但是
invokeAll
可能是一个更好的选择。我想到了这一点,但是invokeAll需要一个可调用的集合。;)实际上,一个Callable是可以被调用的(双关语),因为他希望它们返回一个布尔值。我支持invokeAll()的使用。总是有执行者。可调用()tha