我想编写一个线程,它了解线程已完成并启动新线程。 我的意思是我写了这段代码:
new Thread(new Runnable(){
@Override public void run(){
//code here
}
}).start();
但我想在 for 循环中完成它。 我只想创建 5 个线程。但是当一个线程完成后我想创建一个新线程。
for(int i=0;i<300;i++)
{
//I want to create 5 thread here and stop code and then when a thread has finished I want //to create new thread.
}
最佳答案
线程类有这些方法,可以用来做你想做的事情:
Thread.join()
Thread.isAlive()
但是,您可能确实想使用线程池,如下所示:
ExecutorService executor = Executors.newFixedThreadPool(5);
for(int i=0;i<N;i++) {
executor.submit(new Runnable() {
@Override
public void run() {
}
});
}
关于java - 如何编写一个循环来了解线程已完成并启动新线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17297032/