多线程之生产者消费者

/*
 * 以生产馒头,消耗馒头为例
 */
public class ProduceConsume {  
  
    public static void main(String[] args) {  
        SyncStack ss = new SyncStack();//建造一个装馒头的框  
        Producer p = new Producer(ss);//新建一个生产者,使之持有框  
        Consume c = new Consume(ss);//新建一个消费者,使之持有同一个框  
         new Thread(p).start();//新建一个生产者线程并启动  
         try {
			Thread.sleep(1000);//造成一个生产线程和消费线程的时间差,就不会产生没生产就消费的情况
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
         new Thread(c).start();;//新建一个消费者线程并启动    
    }  
  
}  
  
//馒头类  
class SteamBread{  
    int id;//馒头编号  
    SteamBread(int id){  
        this.id = id;  
    }  
    public String toString(){  //重写toString方法
        return "steamBread:"+id;  
    }  
}  
  
//装馒头的框,栈结构  
class SyncStack{  
    int index = 0;  
    SteamBread[] stb = new SteamBread[6];//构造馒头数组,相当于馒头筐,容量是6  
      
    //放入框中,相当于入栈  
    public synchronized void push(SteamBread sb){  
        while(index==stb.length){//筐满了,即栈满,  
            try {  
                this.wait();//让当前线程等待  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        this.notify();//唤醒在此对象监视器上等待的单个线程,即消费者线程  
        stb[index] = sb;  
        index++;  
    }  
      
    //从框中拿出,相当于出栈  
    public synchronized SteamBread pop(){  
        while(index==0){//筐空了,即栈空  
            try {  
                this.wait();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        this.notify();  
        index--;//push第n个之后,index++,使栈顶为n+1,故return之前要减一   
        return stb[index];  
    }  
}  
  
//生产者类,实现了Runnable接口,以便于构造生产者线程  
class Producer implements Runnable{  
    SyncStack ss = null;  
    Producer(SyncStack ss){  
        this.ss = ss;  
    }  
    @Override  
    public void run() {  
        // 开始生产馒头  
        for(int i=0;i<10;i++){  
            SteamBread stb = new SteamBread(i);  
            ss.push(stb);  
           System.out.println("生产了"+stb);  
            try {  
                Thread.sleep((int)(Math.random()*1000));//每生产一个馒头,睡觉随机秒  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}  
  
//消费者类,实现了Runnable接口,以便于构造消费者线程  
class Consume implements Runnable{  
    SyncStack ss = null;  
    public Consume(SyncStack ss) {  
        this.ss = ss;  
    }  
    @Override  
    public void run() {  
        for(int i=0;i<10;i++){//开始消费馒头  
            SteamBread stb = ss.pop();  
            System.out.println("消费了"+stb);  
            try {  
                Thread.sleep((int)(Math.random()*1000));//每消费一个馒头,睡觉随机秒。 
            } catch (InterruptedException e) {   
                e.printStackTrace();  
            }  
        }  
    }     
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值