zoukankan      html  css  js  c++  java
  • JAVA 线程Join

    join方法: 当某个线程要等待另一个线程执行结束后才能继续执行时,使用join方法。

    public class DinnerThread {
    	
    	public static void main(String[] args){
    		DinnerThread thread = new DinnerThread();
    		Dinner fatherThread = thread.new Dinner();
    		Thread myThread = new Thread(fatherThread);
    		myThread.start();
    		
    	}
    
    	public class Dinner implements Runnable
    	{
    
    		public void run() {
    			System.out.println("去饭店吃饭");
    			System.out.println("点完菜让饭店做菜:");
    			Thread restaurantThread = new Thread(new RestaurantThread());
    			restaurantThread.start();
    			try {
    				restaurantThread.join();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			System.out.println("开始吃饭");	
    		}
    		
    	}
    	
    	public class RestaurantThread implements Runnable{
    
    		public void run() {
    			System.out.println("饭店开始做菜");
    			for(int i=0;i<10;i++){
    				System.out.println("饭店做菜("+(i+1)+")...");
    				try {
    					Thread.sleep(1000);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    			System.out.println("饭店上菜");
    			
    		}
    		
    	}
    }
    

     打印结果如下:

    去饭店吃饭
    点完菜让饭店做菜:
    饭店开始做菜
    饭店做菜(1)...
    饭店做菜(2)...
    饭店做菜(3)...
    饭店做菜(4)...
    饭店做菜(5)...
    饭店做菜(6)...
    饭店做菜(7)...
    饭店做菜(8)...
    饭店做菜(9)...
    饭店做菜(10)...
    饭店上菜
    开始吃饭

  • 相关阅读:
    linux系统telnet端口不通能收到SYN但不回SYN+ACK响应问题排查(转载)
    leveldb
    SSTable and Log Structured Storage: LevelDB
    fio terse输出详解
    bash的循环中无法保存变量
    怎样当好一个师长
    共享变量的并发读写
    Storage Systems topics and related papers
    Storage System and File System Courses
    调试std::string
  • 原文地址:https://www.cnblogs.com/foxting/p/8434666.html
Copyright © 2011-2022 走看看