生产者消费者模式:管程法,信号灯法

管程法

package cooperation;
/**
 * 协作模型:生产者消费者实现方式:管程法
 * 借助缓冲区
 * @author DELL
 *
 */
public class CoTest01 {
	public static void main(String[] args) {
		SynContainer container=new SynContainer();//缓冲区
		new Productor(container).start();
		new Consumer(container).start();
	}
}
//生产者
class Productor extends Thread{
	SynContainer container;
	public Productor(SynContainer container) {
		this.container = container;
	}
	public void run() {
		//生产
		for(int i=0;i<100;i++) {
			System.out.println("生产-->"+i+"个馒头");
			container.push(new Steamedbun(i));
		}
	}
}
//消费者
class Consumer extends Thread{
	SynContainer container;
	public Consumer(SynContainer container) {
		this.container = container;
	}
	public void run() {
		//消费
		for(int i=0;i<100;i++) {
			System.out.println("消费-->"+container.pop().id+"个馒头");
		}
	}
}
//缓冲区
class SynContainer{
	Steamedbun[] buns=new Steamedbun[10];
	int count=0;//计数器
	//存储 生产
	public synchronized void push(Steamedbun bun) {
		//何时能生产 容器存在空间
		//不能生产 只有等待
		if(count==buns.length) {
			try {
				this.wait();//线程阻塞 修奥飞着通知生产解除
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		//存在空间可以生产
		buns[count]=bun;
		count++;
		//存在数据了可以通知消费了
		this.notifyAll();
	}
	//获取 消费
	public synchronized Steamedbun pop() {
		//何时消费 容器中是否存在数据
		//没有数据 只有等待
		if(count==0) {
			try {
				this.wait();//线程阻塞 生产者通知消费解除阻塞
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//存在数据可以消费
		count--;
		Steamedbun bun=buns[count];
		this.notifyAll();//存在空间了,可以唤醒对方生产了
		return bun;
	}
}
//馒头
class Steamedbun{
	int id;
	public Steamedbun(int id) {
		this.id = id;
	}
}

信号灯法

package cooperation;

public class CoTest02 {
	public static void main(String[] args) {
		Tv tv=new Tv();
		 new Player(tv).start();
		 new Watcher(tv).start();
	}
}
//生产者 演员
class Player extends Thread{
	Tv tv;
	public Player(Tv tv) {
		this.tv = tv;
	}
	public void run() {
		for(int i=0;i<100;i++) {
			if(i%20==0) {
				this.tv.paly("奇葩说");
			}else {
				this.tv.paly("太污了");
			}
		}
	}
}
//消费者 观众
class Watcher extends Thread{
	Tv tv;
	public Watcher(Tv tv) {
		this.tv = tv;
	}
	public void run() {
		for(int i=0;i<20;i++) {
			tv.watch();
		}
	}
}
//同一个资源 电视
class Tv{
	String voice;
	//信号灯
	//T表示演员表演,观众等待
	//F表示观众观看,演员等待
	boolean flag=true;
	
	//表演
	public synchronized void paly(String voice) {
		//演员等待
		if(!flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//表演时刻
		System.out.println("表演了"+voice);
		this.voice=voice;
		//唤醒
		this.notifyAll();
		this.flag=!this.flag;
	}
	//观看
	public synchronized void watch() {
		//观众等待
		if(flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//观看
		System.out.println("听到了"+voice);
		//唤醒
		this.notifyAll();
		//切换标志
		this.flag=!this.flag;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值