Java 在所有线程完成执行后,如何运行最后一个要运行的线程?
假设我有X个线程以并行模式运行,同时我希望我的新线程只在所有X个线程完成后运行 如果需要多个阶段,每个阶段的线程数不同,请使用a或a。您可以使用Java 在所有线程完成执行后,如何运行最后一个要运行的线程?,java,multithreading,Java,Multithreading,假设我有X个线程以并行模式运行,同时我希望我的新线程只在所有X个线程完成后运行 如果需要多个阶段,每个阶段的线程数不同,请使用a或a。您可以使用倒计时闩锁。只是 在闩锁构造函数中设置n 在每个辅助线程结束时,使用yourLatchInstance.countDown() 在等待线程开始时,使用await()。在countDown()之后,将被调用n次等待线程将被释放 试试这个: public class Main { /** The initial number of th
倒计时闩锁。只是
- 在闩锁构造函数中设置
n
- 在每个辅助线程结束时,使用
yourLatchInstance.countDown()
- 在等待线程开始时,使用
await()
。在countDown()之后,
将被调用n
次等待线程将被释放
试试这个:
public class Main {
/** The initial number of threads launched*/
public static final int INITIAL_THREADS = 10;
/** When less than MIN_THREADS are running, a new Thread is thrown. */
public static final int MIN_THREADS = 5;
/** */
public static final int TOTAL_THREADS_TO_PROCESS = 30;
/** Launches INITIAL_THREADS and ends */
public static void main(String[] args){
for(int i=0; i<INITIAL_THREADS; i++)
new Thread( new MyThread() ).start();
}
}
class MyThread implements Runnable{
/** Stores the number of Threads runnning running. */
private static int threadsRunning = 0;
/** Stores the number of total thread processed. Used as a exit confition */
private static int threadProcessed = 0;
@Override
public void run(){
//With this synchronized block we modify the threadsRunning safely
//synchronized(this) <- Not valid because Threads objects are
// not the same instance.
synchronized(MyThread.class){
threadsRunning++;
threadProcessed++;
System.out.println("Threads running:" + threadsRunning +
", Total Threads processed:" + threadProcessed +".");
}
//Thread Code here. I simulate it with a 10 second sleep.
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Needed to read/write threadsRunning and threadProcessed safely
synchronized(MyThread.class){
threadsRunning--;
if(threadsRunning < Main.MIN_THREADS &&
threadProcessed < Main.TOTAL_THREADS_TO_PROCESS)
new Thread( new MyThread() ).start();
}
}
}
公共类主{
/**启动的线程的初始数目*/
公共静态final int INITIAL_线程=10;
/**当运行的线程少于MIN_时,将抛出一个新线程。*/
公共静态final int MIN_线程=5;
/** */
公共静态final int TOTAL_THREADS_TO_PROCESS=30;
/**启动初始线程并结束*/
公共静态void main(字符串[]args){
对于(int i=0;iDid您自己启动线程吗?因为这样您可以保留一个启动线程数的计数器,让它们在完成时回调以减少计数器,如果最后一个计数器将计数器设置为0,则运行您的新线程。如果您认为在所有线程之后撒尿不会,那么您将无法创建复杂的程序terminated不同于线程终止后执行poos的问题。开始练习。
public class Main {
/** The initial number of threads launched*/
public static final int INITIAL_THREADS = 10;
/** When less than MIN_THREADS are running, a new Thread is thrown. */
public static final int MIN_THREADS = 5;
/** */
public static final int TOTAL_THREADS_TO_PROCESS = 30;
/** Launches INITIAL_THREADS and ends */
public static void main(String[] args){
for(int i=0; i<INITIAL_THREADS; i++)
new Thread( new MyThread() ).start();
}
}
class MyThread implements Runnable{
/** Stores the number of Threads runnning running. */
private static int threadsRunning = 0;
/** Stores the number of total thread processed. Used as a exit confition */
private static int threadProcessed = 0;
@Override
public void run(){
//With this synchronized block we modify the threadsRunning safely
//synchronized(this) <- Not valid because Threads objects are
// not the same instance.
synchronized(MyThread.class){
threadsRunning++;
threadProcessed++;
System.out.println("Threads running:" + threadsRunning +
", Total Threads processed:" + threadProcessed +".");
}
//Thread Code here. I simulate it with a 10 second sleep.
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Needed to read/write threadsRunning and threadProcessed safely
synchronized(MyThread.class){
threadsRunning--;
if(threadsRunning < Main.MIN_THREADS &&
threadProcessed < Main.TOTAL_THREADS_TO_PROCESS)
new Thread( new MyThread() ).start();
}
}
}