生产者消费者问题(以面包为例)

package  Thread;

public   class  ProducerConsumer 
{
    
public static void main(String[] args) 
    
{
        SynchronizedStack ss 
= new SynchronizedStack();
        Producer p 
= new Producer(ss); //产生一个生产者
        Consumer c = new Consumer(ss); //产生一个消费者
        new Thread(p).start(); //启动生产者线程
        new Thread(c).start(); //启动消费者线程
        
    }


}


class  Bread  // 定义生产面包
{
    
int id;
    Bread(
int id)
    
{
        
this.id = id;
    }

    
    
public String toString() //重写toString方法
    {
        
return "bread :" + id;
    }

}


class  SynchronizedStack  // 定义一个盛面包的容器
{
    
int index = 0;
    Bread[] bread 
= new Bread[6];
    
    
public synchronized void putIn(Bread bread) // 放进方法
    {
        
while(index == this.bread.length)
        
{
            
try 
            
{
                
this.wait(); // 只针对synchronized
//Object对象的wait()方法,表示调用当前对象的wait()方法,运行当前对象的此方法的线程等待,直到被notify()唤醒
            }
 
            
catch (InterruptedException e) 
            
{
                e.printStackTrace();
            }

        }

        
this.notify(); //唤醒一个wait的线程
//        this.notifyAll();//唤醒所有wait的线程
        this.bread[index] = bread;
        index 
++;
    }

    
    
public synchronized Bread putOut() // 拿出方法
    {
        
while(index == 0)
        
{
            
try 
            
{
                
this.wait();
            }
 
            
catch (InterruptedException e) 
            
{
                e.printStackTrace();
            }

        }

        
this.notify(); //唤醒一个wait的线程
//        this.notifyAll();//唤醒所有wait的线程
        index --;
        
return bread[index];
    }

}


class  Producer  implements  Runnable
{
    SynchronizedStack ss 
= null;
    Producer(SynchronizedStack ss)
    
{
        
this.ss = ss;
    }

    
public void run() 
    
{
        
for(int i=0;i<30;i++)
        
{
            Bread bread 
= new Bread(i);
            ss.putIn(bread);
            System.out.println(
"生产了 :" + bread);
            
try
            
{
//                Thread.sleep(1000);
                Thread.sleep((int)Math.random() * 1000);
            }

            
catch(InterruptedException e)
            
{
                e.printStackTrace();
            }

        }

    }

}


class  Consumer  implements  Runnable
{
    SynchronizedStack ss 
= null;
    Consumer(SynchronizedStack ss)
    
{
        
this.ss = ss;
    }

    
public void run() 
    
{
        
for(int i=0;i<30;i++)
        
{
            Bread bread 
= ss.putOut();
            System.out.println(
"消费了 :" + bread);
            
try
            
{
//                Thread.sleep(1000);
                Thread.sleep((int)Math.random() * 1000);
            }

            
catch(InterruptedException e)
            
{
                e.printStackTrace();
            }

        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值