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服务
由于
未来
接口,这要容易得多。这是正确的方法;我将添加一些关于处理执行中错误的内容-使用
执行服务
由于
未来
接口,这要容易得多。