//////////////////////////////////////////
class Product{//定义产品类 int id=0; Product(int id){ this.id=id; } }
class Storage{//定义仓库
int occupiedBufferCount=0;//说明仓库放满的数目,刚开始全是空的,一个产品也没放 Product[] buffer=new Product[6];//创建6个缓冲区,用来放产品
//c++中,用p、v操作来实现同步与互斥,在java中,用synchronized来实现同步,
public synchronized void push(Product m){//同步方法,仓库中的放东西方法
while(occupiedBufferCount==buffer.length){//生产者要放东西,当通通放满时,必须等待, try{
System.out.println(
Thread.currentThread().getName()+\" tries to produce,but it's full,only to wait\"); this.wait();
}catch(InterruptedException e){
System.out.println(\"Throw InterrupedEXception\"); e.printStackTrace();
}//有可能抛出异常,必须对它进行捕捉 }
notifyAll();//如果没有满,则生产者顺利放入产品
buffer[occupiedBufferCount]=m; occupiedBufferCount++; }
public synchronized Product get(){
while(occupiedBufferCount==0){//消费者要拿东西,却发现什么都没有 try{
System.out.println(
Thread.currentThread().getName()+\" tries to consumer,but it's nothing,only to wait\"); this.wait();
}catch(InterruptedException e){
System.out.println(\"Throw InterrupedEXception\"); e.printStackTrace();
}//有可能抛出异常,必须对它进行捕捉 }
occupiedBufferCount--;
notifyAll();//若成功拿取,则把空位置的数目减1,并返回产品
return buffer[occupiedBufferCount]; } }
class Producer implements Runnable{//定义生产者线程 Storage s;//声明生产者要放产品的仓库 Producer(Storage s){ this.s=s; static int i=0;
public void run(){//定义一个新线程必须重写父类的run方法
for( i++;i <=40;i++){ if(s.occupiedBufferCount <6){
Product m=new Product(i);//生产20个产品 s.push(m);
System.out.println(
Thread.currentThread().getName()+\"生产了\"+m.id+\" 总数:\"+s.occupiedBufferCount); try{
Thread.sleep((int)(Math.random()*200));//这里的休眠时间越长用来模拟它生产的越慢 } catch(InterruptedException e){ e.printStackTrace(); } } else try{
System.out.println(
Thread.currentThread().getName()+\" tries to produce,but it's full full,only to wait\"); this.wait();
}catch(InterruptedException e){
System.out.println(\"Throw InterrupedEXception\"); e.printStackTrace();
}//有可能抛出异常,必须对它进行捕捉 /* */ } } }
class Consumer implements Runnable{ Storage s;
Consumer(Storage s){ this.s=s; }
public void run(){
for(int i=0;i <20;i++){ if(s.occupiedBufferCount>0){ Product m= s.get(); System.out.println(
Thread.currentThread().getName()+\"消费了\"+m.id+\" 总数:\"+s.occupiedBufferCount); try{
Thread.sleep((int)(Math.random()*1000));//这里的休眠时间越短用来模拟它生产的越快 }catch(InterruptedException e){ e.printStackTrace(); } }
else try{ System.out.println(
Thread.currentThread().getName()+\" tries to consumer,but it's nothing nothing,only to wait\"); this.wait(); }
catch(InterruptedException e){
System.out.println(\"Throw InterrupedEXception\"); e.printStackTrace();
}//有可能抛出异常,必须对它进行捕捉/**/ } } }
public class ProducerAndConsumer3{ public static void main(String[] args){ Storage s=new Storage(); Producer p=new Producer(s); Consumer c=new Consumer(s); new Thread(p,\"producer1\").start(); new Thread(c,\"consumer1\").start();
//下面的是用来模拟多线程,这样总共有三个生产者,三个消费者
new Thread(p,\"producer2\").start(); new Thread(c,\"consumer2\").start(); /* new Thread(p,\"producer3\").start(); new Thread(c,\"consumer3\").start(); */ } }
因篇幅问题不能全部显示,请点此查看更多更全内容