JAVA之wait&notify&Condition

         有某个线程唤醒等待中的线程,JDK1.5之前可有synchronized、wait()、notify()/notifyAll()实现,JDK1.5提供了Lock、Condition组合实现。

        例如,妈妈往盘子中放一个鸡蛋,则小明拿来吃。如果盘子是空的,则小明进入等待状态,如果盘子还有鸡蛋,则妈妈进入等待状态,当妈妈放如一个鸡蛋时,唤醒小明吃,小明吃了鸡蛋之后,唤醒妈妈放鸡蛋。

package thread;

public class CommunicationThreadTest {

	public static void main(String[] args) {
		Egg egg = new Egg();
		new MyThread(egg, "mother").start();
		new MyThread(egg, "xiaoming").start();
	}
}

class MyThread extends Thread {
	private Egg egg;
	private String name;

	public MyThread(Egg egg, String name) {
		this.egg = egg;
		this.name = name;
	}

	@Override
	public void run() {
		while (true) {
			if ("mother".equals(name)) {
				egg.put(name);
			} else {
				egg.eat(name);
			}
		}
	}
}

class Egg {
	private int eggNum = 0;

	public synchronized void put(String name) {
		while (eggNum > 0) {
			try {
				System.out.println(name + " 进入等待状态....");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		eggNum = eggNum + 1;
		System.out.println(name + " 放了1个鸡蛋....");
		this.notifyAll();
	}

	public synchronized void eat(String name) {
		while (eggNum <= 0) {
			try {
				System.out.println(name + "进入等待状态....");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		eggNum--;
		System.out.println(name + " 吃了一个鸡蛋.....");
		this.notifyAll();
	

运行结果


xiaoming进入等待状态....
mother 放了1个鸡蛋....
mother 进入等待状态....
xiaoming 吃了一个鸡蛋.....

 

    用Lock及Condition修改

package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CommunicationThreadTest {

	public static void main(String[] args) {
		Egg egg = new Egg();
		new MyThread(egg, "mother").start();
		new MyThread(egg, "xiaoming").start();
	}
}

class MyThread extends Thread {
	private Egg egg;
	private String name;

	public MyThread(Egg egg, String name) {
		this.egg = egg;
		this.name = name;
	}

	@Override
	public void run() {
		while (true) {
			if ("mother".equals(name)) {
				egg.put(name);
			} else {
				egg.eat(name);
			}
		}
	}
}

class Egg {
	private int eggNum = 0;
    Lock lock=new ReentrantLock();
    Condition condition=lock.newCondition();
	public  void put(String name) {
		 lock.lock();
			try {
				while (eggNum > 0) {
				System.out.println(name + "进入等待状态....");
				condition.await();//这里要注意,不要选择wait(),wait()是Object中的
				}
				eggNum++;
				System.out.println(name + " 放了一个鸡蛋.....");
				condition.signalAll();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
	}

	public  void eat(String name) {
		   lock.lock();
			try {
				while (eggNum <= 0) {
				System.out.println(name + "进入等待状态....");
				condition.await();//这里要注意,不要选择wait(),wait()是Object中的
				}
				eggNum--;
				System.out.println(name + " 吃了一个鸡蛋.....");
				condition.signalAll();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
	}
}

运行结果

mother 放了一个鸡蛋.....
mother进入等待状态....
xiaoming 吃了一个鸡蛋.....
mother 放了一个鸡蛋.....
mother进入等待状态....

 
 
 
 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值