java实现 生产者和消费者问题 多线程同步示例

package  com.producerconsumer;

/**
 * <p>
 * Title:产品
 * </p>
 * 
 * 
@author  孙钰佳
 * @main sunyujia@yahoo.cn
 * @date 2008-3-16 下午02:50:12
 
*/
public   class  Produce {

    
/**
     * 产品名称
     
*/
    
private  String name;

    
public  Produce(String name) {
        
super ();
        
this .name  =  name;
    }

    
public  String getName() {
        
return  name;
    }

    
public   void  setName(String name) {
        
this .name  =  name;
    }
}
 
package  com.producerconsumer;

/**
 * <p>
 * Title: 产品仓库
 * </p>
 * 
 * 
 * 
@author  孙钰佳
 * @main sunyujia@yahoo.cn
 * @date 2008-3-16 下午02:54:50
 
*/
public   class  Storage {
    
private   final   static   int  SIZE  =   100 ; //  队列大小

    
private  Produce[] produces  =   new  Produce[SIZE]; //  模拟队列

    
private   int  front  =   0 ; //  头下标

    
private   int  rear  =   0 ; //  尾下标

    
public  Produce getProduce() {
        
synchronized  ( this ) {
            
while  (front  ==  rear) {
                
try  {
                    System.out.println(
" 无可消费的产品! " );
                    
this .wait();
                } 
catch  (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Produce produce 
=  produces[front];
            front 
=  (SIZE  +   1   +  front)  %  SIZE; //  从1开始,最大size-1
            System.out.println( " 消费=> "   +  produce.getName());
            
this .notifyAll();
            
return  produce;
        }
    }

    
public   void  putProduce(Produce produce) {
        
synchronized  ( this ) {
            
while  ((rear  +   1 %  SIZE  ==  front) {
                
try  {
                    System.out.println(
" 仓库已满! " );
                    
this .wait();
                } 
catch  (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            produces[rear] 
=  produce;
            rear 
=  (rear  +   1 %  SIZE; //  从0开始,最大size-1
            System.out.println( " 生产=> "   +  produce.getName());
            
this .notifyAll();
        }

    }
}
package  com.producerconsumer;

/**
 * <p>
 * Title: 生成者
 * </p>
 * 
 * 
@author  孙钰佳
 * @main sunyujia@yahoo.cn
 * @date 2008-3-16 下午02:53:44
 
*/
public   class  Producer  extends  Thread {
    
private  Storage storage;

    
public  Producer(Storage storage) {
        
super ();
        
this .storage  =  storage;
    }

    
public   void  run() {
        
while  ( true ) {
            Produce produce 
=   new  Produce( " syj " );
            storage.putProduce(produce);
            
//  System.out.println("生产:" + produce.getName());
             try  {
                sleep((
int ) (Math.random()  *   10000 ));
            } 
catch  (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package  com.producerconsumer;

/**
 * <p>
 * Title:消费者
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * 
 * 
@author  孙钰佳
 * @main sunyujia@yahoo.cn
 * @date 2008-3-16 下午03:06:56
 
*/
public   class  Consumer  extends  Thread {
    
private  Storage storage;

    
public  Consumer(Storage storage) {
        
super ();
        
this .storage  =  storage;
    }

    
public   void  run() {
        
while  ( true ) {
            Produce produce 
=  storage.getProduce();
            
//  System.out.println("消费:" + produce.getName());
             try  {
                sleep((
int ) (Math.random()  *   10000 ));
            } 
catch  (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package  com.producerconsumer;

/**
 * <p>
 * Title: 测试生产者消费者
 * </p>
 * 
 * 
@author  孙钰佳
 * @main sunyujia@yahoo.cn
 * @date 2008-3-16 下午03:11:05
 
*/
public   class  Test {
    
public   static   void  main(String[] args) {
        Storage storage 
=   new  Storage();

        Producer producer1 
=   new  Producer(storage);
        Producer producer2 
=   new  Producer(storage);
        Producer producer3 
=   new  Producer(storage);
        Producer producer4 
=   new  Producer(storage);

        Consumer consumer1 
=   new  Consumer(storage);
        Consumer consumer2 
=   new  Consumer(storage);
        Consumer consumer3 
=   new  Consumer(storage);
        Consumer consumer4 
=   new  Consumer(storage);
        Consumer consumer5 
=   new  Consumer(storage);
        Consumer consumer6 
=   new  Consumer(storage);

        producer1.start();
        producer2.start();
        consumer1.start();
        consumer2.start();
        consumer3.start();
        producer3.start();
        producer4.start();
        consumer4.start();
        consumer5.start();
        consumer6.start();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值