Java实现同步机制(生产者消费者)

Java实现线程的同步,可以通过synchronized,wait(), notitfy(), notifyAll();假设一个线程(生产者)生产产品,一个线程(消费者)消费产品,其访问的资源时间都是随机的,这样就是生产者必须得产品(资源)消费完成之后才可以生产,而消费者必须在产品有的时候才可以消费,这就是必须对资源进行同步操作,对资源的使用部分的代码需要加入锁。

下列是我的实现方法:

    package com.lzb.common;    
        
    import java.util.Random;    
    import java.util.concurrent.TimeUnit;    
    /**  
     *   
     * 功能描述:生产者消费者  
     *          注:锁synhronized是放在“资源的类的内部方法中”,而不是在线程的代码中  
     */    
    public class ProducterCustomer {    
            
        private PCResource pc = new PCResource();    
        // 生产者与消费者调用的时间随机    
        private Random rand = new Random(50);    
            
        public void init() {            
            // 生产者    
            new Thread(new Runnable(){    
        
                public void run() {                 
                    while(true) {    
                        pc.producter();    
                        try {    
                            TimeUnit.MILLISECONDS.sleep(rand.nextInt(1000));    
                        } catch (InterruptedException e) {    
                            e.printStackTrace();    
                        }    
                    }    
                        
                }}).start();    
            
            // 消费者    
            new Thread(new Runnable(){    
        
                public void run() {    
                    while(true) {    
                        pc.customer();    
                        try {    
                            TimeUnit.MILLISECONDS.sleep(rand.nextInt(1000));    
                        } catch (InterruptedException e) {    
                            e.printStackTrace();    
                        }    
                    }    
                }}).start();    
        }    
            
        public static void main(String[] args) {    
                
            ProducterCustomer startPc = new ProducterCustomer();    
            startPc.init();    
        }           
    }    
        
    /**  
     *   
     * 功能描述:同步资源  
     *  
     */    
    class PCResource {    
            
        private static final Integer MAX = 1;    
        private static final Integer MIN = 0;    
        private int product = 0;    
        // 同步互斥通信标志    
        private boolean isRunning = true;    
            
        /**  
         *   
         * 功能描述:生产产品,当生产一个商品后挂起,等待消费者消费完成  
         */    
        public synchronized void producter() {    
                
            while(isRunning) {    
                try {    
                    wait();    
                } catch (InterruptedException e) {    
                    e.printStackTrace();    
                }    
                product++;    
                System.out.println("-------->Product " + product + " good");    
                if(product >= MAX)    
                    break;    
            }    
            isRunning = false;    
            notify();    
        }    
            
        /**  
         *   
         * 功能描述:消费者,消费产品,当产品为0时,等待生产者生产产品  
         */    
        public synchronized void customer() {    
                
            while(!isRunning) {    
                try {    
                    wait();    
                } catch (InterruptedException e) {    
                    e.printStackTrace();    
                }    
                product--;    
                System.out.println("Limit " + product + " goods<----------");    
                if(product <= MIN) {    
                    break;    
                }    
            }    
            isRunning = true;    
            notify();    
        }    
    }    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值