zoukankan      html  css  js  c++  java
  • 生产者---消费者问题

    一、管程法

    /**
     * 并发协作模型:生产者消费者实现方式:管程法
     * 借助缓冲区
     * @author DARK
     *
     */
    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;
    	}
    	//生产
    	@Override
    	public void run() {
    		for(int i=1;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;
    	}
    	//消费
    	@Override
    	public void run() {
    		for(int i=1;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) {
    				e.printStackTrace();
    			}
    		}
    		buns[count]=bun;
    		count++;
    		this.notifyAll();//有数据 唤醒消费
    	}
    	//获取 消费
    	public synchronized SteamedBun pop() {
    		//何时消费 容器中是否存在数据
    		//有数据 消费,无数据 等待
    		if(count==0) {
    			try {
    				this.wait();//线程阻塞。生产者通知消费解除
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		count--;
    		SteamedBun bun=buns[count];
    		this.notifyAll();//有空间 唤醒生产
    		return bun;
    	}
    }
    //数据
    class SteamedBun{
    	int id;
    	public SteamedBun(int id) {
    		this.id = id;
    	}
    }
    

    二、信号灯法

    /**
     * 生产者消费者模式  信号灯法
     * 借助标志位
     * @author DARK
     *
     */
    public class CoTest02 {
    	public static void main(String[] args) {
    		Tv tv=new Tv();
    		
    		new Actor(tv).start();
    		new Watcher(tv).start();
    	}
    }
    //生产者 演员
    class Actor extends Thread{
    	Tv tv;
    	public Actor(Tv tv) {
    		this.tv=tv;
    	}
    	@Override
    	public void run() {
    		for(int i=0;i<20;i++) {
    			if(i%2==0) {
    				tv.play("奇葩说");
    			}else {
    				tv.play("吐槽大会");
    			}
    		}
    	}
    }
    //消费者 观众
    class Watcher extends Thread{
    	Tv tv;
    	public Watcher(Tv tv) {
    		this.tv=tv;
    	}
    	@Override
    	public void run() {
    		for(int i=0;i<20;i++) {
    			tv.listen();
    		}
    	}
    }
    //同一个资源 电视
    class Tv{
    	String voice;
    	//信号灯 T:演员表演,观众等待 F:观众观看,演员等待
    	boolean flag=true;
    	//表演
    	public synchronized void play(String voice) {
    		//演员等待
    		if(!flag) {
    			try {
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		//表演时刻
    		System.out.println("表演了:"+voice);
    		this.voice=voice;
    		this.notifyAll();
    		//切换标志
    		this.flag=!this.flag;
    	}
    	//观看
    	public synchronized void listen() {
    		//观众等待
    		if(flag) {
    			try {
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		//观看时刻
    		System.out.println("听到了:"+voice);
    		this.notifyAll();
    		//切换标志
    		this.flag=!this.flag;
    	}
    }
    

      

  • 相关阅读:
    Codeforces Round #447 Div. 2 A.B.C
    Codeforces Round #445 Div. 2 A ACM ICPC+B Vlad and Cafes
    51Nod 1035 最长的循环节 数论
    Codeforces Round #444 (Div. 2) C.Solution for Cube 模拟
    POJ 3111 K Best
    POJ 2976 Dropping tests
    POJ 3045 Cow Acrobats
    POJ 3045 Cow Acrobats
    POJ 3273
    POJ 3258 River Hopscotch
  • 原文地址:https://www.cnblogs.com/firecode7/p/13431729.html
Copyright © 2011-2022 走看看