预防死锁的生产者与消费者模型

协作模型:生产者消费者实现的方式之一:管程法

  • 角色
  • 生产者
  • 消费者
  • 缓冲区
  • 数据
public class Cotest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SynContainer container=new SynContainer();
		 new Productor(container).start();
		 new Consumer(container).start();
	}

}
//生产者
class Productor extends Thread{
	SynContainer containers;//生产者消费者操作的是一个缓冲区

	public Productor(SynContainer containers) {
		super();
		this.containers = containers;
	}
	
	@Override
	public void run() {
	//生产
		for(int i=0;i<100;i++) {
			System.out.println("生产第"+i+"个馒头");
			containers.push(new Streamedbun(i));
		}
	}
	
}
//消费者
class Consumer extends Thread{
	SynContainer containers;

	public Consumer(SynContainer containers) {
		super();
		this.containers = containers;
	}
	@Override
	public void run() {
		for(int i=0;i<100;i++) {//消费的多,会出现等待现象
			System.out.println("消费第"+containers.pop().getId()+"个馒头");
		}
	}
	
}
//缓冲区:自己实现的容器,没有使用并发容器:CopyOnWriteArrayList<E>
//存储
//取
class SynContainer {
	Streamedbun[] buns=new Streamedbun[10];//存储容器
	int count=0;//计数器
	
	//存储
	public synchronized void push(Streamedbun 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 Streamedbun pop() {
		//何时消费:容器中是否存在数据
		//没有数据,只有等待
		if(count==0) {
			try {
				this.wait();//线程阻塞,生产者通知消费,阻塞解除
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//存在数据
		count--;
		Streamedbun bun=buns[count];//从最后一个位置拿数据
		this.notifyAll();//存在空间,唤醒对方生产
		return bun;
	}
}
//数据:馒头
class Streamedbun{
	private int id;

	public Streamedbun(int id) {
		super();
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	}
	

协作模型:生产者消费者实现的方式之一:信号灯法

  • 借助的是标志位
  • 角色
  • 生产者
  • 消费者
public class CoLight {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Tv tv=new Tv();
		new Player(tv).start();
		new Audience(tv).start();
	}

}
//生产者 演员

class Player extends Thread{
	Tv tv;

	public Player(Tv tv) {
		super();
		this.tv = tv;
	}
	@Override
	public void run() {
		for(int i=0;i<20;i++) {
			if(i%2==0) {
				this.tv.play("奇葩说");
			}else {
				this.tv.play("休息一下,进入广告");
			}
		}
	}
}
// 消费者 观众

class Audience extends Thread{
	Tv tv;

	public Audience(Tv tv) {
		super();
		this.tv = tv;
	}
	@Override
	public void run() {
		for(int i=0;i<20;i++) {
				this.tv.watch();
			}
		}
}
//统一资源:电视
class Tv{
	String voice;
	//信号灯,T:表示演员表演,观众等待。F:表示观众观赏,演员等待。
	boolean flag=true;
	//表演
	public synchronized void play(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=false;
	}
	
	//观看
	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=true;
		
	}
	
}
发布了43 篇原创文章 · 获赞 11 · 访问量 2553

猜你喜欢

转载自blog.csdn.net/weixin_43328816/article/details/104415810