String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start...");
Thread firstThread = new Thread(new ThreadOne());
Thread secondThread = new Thread(new ThreadTwo());
Thread thirdThread = new Thread(new ThreadThree());
// 2. thread run in order
try {
firstThread.start();
firstThread.join(); // 让别的线程等待这个线程结束
secondThread.start();
secondThread.join();
thirdThread.start();
thirdThread.join();
} catch (Exception ex) {
System.out.println("thread join exception.");
}
System.out.println(threadName + " end.");
System.out.println(threadName + " start...");
Thread firstThread = new Thread(new ThreadOne());
Thread secondThread = new Thread(new ThreadTwo());
Thread thirdThread = new Thread(new ThreadThree());
// 2. thread run in order
try {
firstThread.start();
firstThread.join(); // 让别的线程等待这个线程结束
secondThread.start();
secondThread.join();
thirdThread.start();
thirdThread.join();
} catch (Exception ex) {
System.out.println("thread join exception.");
}
System.out.println(threadName + " end.");

本文介绍了一个Java程序中如何通过Thread类创建多个线程,并确保它们按特定顺序执行的方法。使用了start()方法启动线程,通过join()方法实现主线程等待其他线程执行完毕后再继续执行。
5172

被折叠的 条评论
为什么被折叠?



