Java 如何确保所有子类线程都已退出?
拥有-Java 如何确保所有子类线程都已退出?,java,multithreading,runnable,Java,Multithreading,Runnable,拥有- for (int i = 0; i<10 ; i++) { Runnable r = new Runnable(){...} new Thread(r).start(); } // I want to continue here only after all the subclass threads before exited . ... 用于(int i=0;i看一看。当你想等待N个线程完成某件事情时,它非常棒
for (int i = 0; i<10 ; i++) {
Runnable r = new Runnable(){...}
new Thread(r).start();
}
// I want to continue here only after all the subclass threads before exited .
...
用于(int i=0;i看一看。当你想等待N个线程完成某件事情时,它非常棒。看一看。当你想等待N个线程完成某件事情时,它非常棒。看一看。当你想等待N个线程完成某件事情时,它非常棒。看一看。它是great表示您希望等待N个线程完成某项任务的时间
在for部分之后继续之前,如何确保所有子类线程都已退出
我会使用ExecutorService
类。请参见它们上的。类似于:
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// or you can create an open-ended thread pool
// ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
threadPool.submit(new Runnable(){...});
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
如果您仍然希望自己执行线程,则通常将它们放在列表中,并对每个线程调用join()
:
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable(){...});
thread.start();
threadList.add(thread);
}
// this waits for all of the threads to finish before continuing
for (Thread thread : threadList) {
thread.join();
}
List-threadList=new-ArrayList();
对于(int i=0;i<10;i++){
线程线程=新线程(新的Runnable(){…});
thread.start();
线程列表。添加(线程);
}
//这将等待所有线程完成,然后继续
用于(线程:线程列表){
thread.join();
}
在for部分之后继续之前,如何确保所有子类线程都已退出
我会使用ExecutorService
类。请参见它们上的。类似于:
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// or you can create an open-ended thread pool
// ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
threadPool.submit(new Runnable(){...});
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
如果您仍然希望自己执行线程,则通常将它们放在列表中,并对每个线程调用join()
:
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable(){...});
thread.start();
threadList.add(thread);
}
// this waits for all of the threads to finish before continuing
for (Thread thread : threadList) {
thread.join();
}
List-threadList=new-ArrayList();
对于(int i=0;i<10;i++){
线程线程=新线程(新的Runnable(){…});
thread.start();
线程列表。添加(线程);
}
//这将等待所有线程完成,然后继续
用于(线程:线程列表){
thread.join();
}
在for部分之后继续之前,如何确保所有子类线程都已退出
我会使用ExecutorService
类。请参见它们上的。类似于:
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// or you can create an open-ended thread pool
// ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
threadPool.submit(new Runnable(){...});
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
如果您仍然希望自己执行线程,则通常将它们放在列表中,并对每个线程调用join()
:
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable(){...});
thread.start();
threadList.add(thread);
}
// this waits for all of the threads to finish before continuing
for (Thread thread : threadList) {
thread.join();
}
List-threadList=new-ArrayList();
对于(int i=0;i<10;i++){
线程线程=新线程(新的Runnable(){…});
thread.start();
线程列表。添加(线程);
}
//这将等待所有线程完成,然后继续
用于(线程:线程列表){
thread.join();
}
在for部分之后继续之前,如何确保所有子类线程都已退出
我会使用ExecutorService
类。请参见它们上的。类似于:
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// or you can create an open-ended thread pool
// ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
threadPool.submit(new Runnable(){...});
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
如果您仍然希望自己执行线程,则通常将它们放在列表中,并对每个线程调用join()
:
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable(){...});
thread.start();
threadList.add(thread);
}
// this waits for all of the threads to finish before continuing
for (Thread thread : threadList) {
thread.join();
}
List-threadList=new-ArrayList();
对于(int i=0;i<10;i++){
线程线程=新线程(新的Runnable(){…});
thread.start();
线程列表。添加(线程);
}
//这将等待所有线程完成,然后继续
用于(线程:线程列表){
thread.join();
}
这是完全错误的方法。了解使用ExecutorSevice
和Java 5并发API的情况。这是完全错误的方法。了解使用ExecutorSevice
和Java 5并发API的情况。这是完全错误的方法。了解使用ExecutorSevice
和Java 5并发API的情况PI.这是完全错误的方法。请仔细阅读使用ExecutorSevice
和Java 5并发API。我认为CountDownLatch
是错误的方法。我认为ExecutorService
和invokeAll
方法是一种不太容易出错的方法。@BoristheSpider它有它的用途,你gh如果这些线程继续运行会更好。我认为CountDownLatch
在这里是错误的方法。我认为ExecutorService
和invokeAll
方法是一种不太容易出错的方法。@Boristeider它有它的用途,尽管如果这些线程继续运行会更好。我想k.CountDownLatch
在这里是错误的方法。我认为ExecutorService
和invokeAll
方法是一种不太容易出错的方法。@BoristSpider它有它的用途,不过如果这些线程继续下去会更好。我认为CountDownLatch
是错误的方法e、 我认为ExecutorService
和invokeAll
方法是一种不太容易出错的方法。@BoristSpider它有它的用途,但如果这些线程继续运行会更好。这是正确的方法;我将添加一些关于处理执行中的错误的内容-使用ExecutorService
这个方法由于Future
接口,这更容易。这是正确的方法;我将添加一些关于处理执行中的错误的内容-使用ExecutorService
由于Future
接口,这更容易。这是正确的方法;我将添加一些关于处理执行中的错误的内容-使用Executor服务
由于未来
接口,这要容易得多。这是正确的方法;我将添加一些关于处理执行中错误的内容-使用执行服务
由于未来
接口,这要容易得多。