线程让步

/*
 * 线程让步:  join方法
 * 
 * 	需求:模拟小时候  没有盐去买盐
 * 
 * 
 * */

class Mother extends Thread{
	
	@Override
	public void run() {
		System.out.println("老妈洗菜...");
		System.out.println("老妈切菜...");
		System.out.println("老妈发现没有盐了...");
		
		//通知儿子买盐(创建新线程)
		Son son = new Son();
		son.start();
		
		/*
		 *join 加入:如果当前线程执行了join方法,那么当前线程就会让步
		 *给新加入的线程先完成任务,然后当前线程才继续执行自己的任务
		 *
		 */
		try {
			son.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("老妈炒菜...");
		System.out.println("全家一起吃饭...");

	}
	
}

class Son extends Thread{
	
	@Override
	public void run() {
		System.out.println("下楼...");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("走去超市...");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("买盐,砍价...");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("被骂了一顿,拿着盐回来了...");
	}
}

public class Demo9 {

	public static void main(String[] args) {
		Mother t = new Mother();
		t.start();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值