class FatherThread implements Runnable
{
public void run(){
System.out.println("爸爸下班回家.");
System.out.println("爸爸準備洗澡.");
System.out.println("爸爸發現瓦斯沒了.");
System.out.println("爸爸打電話請瓦斯工人送瓦斯.");
Thread worker = new Thread(new WorkerThread());
System.out.println("爸爸等待瓦斯工人...");
worker.start();
try{
//join()方法會暫停目前正在執行的執行緒,值到被join的執行緒執行完畢(或執行一段時間)為止
worker.join();
} catch(InterruptedException ie ){
System.out.println("爸爸決定今天不洗熱水澡了 !");
}
System.out.println("爸爸開始洗澡!");
System.out.println("爸爸洗完澡了!");
}
}
class WorkerThread implements Runnable
{
public void run(){
System.out.println();
System.out.println("瓦斯工人送瓦斯中...");
try{
for(int i=1;i<=5;i++){
Thread.sleep(1000);
System.out.print(i + "分鐘, ");
}
} catch(InterruptedException ie){
System.out.println("瓦斯工人送瓦斯途中發生意外!");
}
System.out.println();
System.out.println("瓦斯工人將瓦斯送到家了!");
System.out.println("瓦斯工人將瓦斯安裝完畢!");
System.out.println();
}
}
public class Shower
{
public static void main(String[] args){
Thread father = new Thread(new FatherThread());
father.start();
}
}
來源:猛虎出閘雙劍合璧版