在学习Lock实现生产者和消费者遇到的阻塞问题

在学习Lock实现生产者和消费者遇到的阻塞问题

产品类:

public class Value {
    public static String value = "";//“”没有产品,非空有产品
}

生产者:

public class Producer {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void setValue() {
        try {
            lock.lock();
            if (!Value.value.equalsIgnoreCase("")) {
                condition.await();
            }
            String value = System.currentTimeMillis() + "_" + System.nanoTime();
            Value.value = value;
            System.out.println(Thread.currentThread().getName()+"生产了value为:" + value);
            condition.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

消费者:

public class Customer {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
 public void getValue() {
        try {
            lock.lock();
            if (Value.value.equalsIgnoreCase("")){
                condition.await();}

            System.out.println(Thread.currentThread().getName()+"消费了value,为:" + Value.value);
            Value.value = "";
            condition.signal();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}

测试方法:

public class TestMain {

    public static void main(String[] args) throws Exception {
        Producer producer = new Producer();
        Customer customer = new Customer();
        Runnable producerRunable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <Integer.MAX_VALUE; i++) {
                    producer.setValue();         
                }
            }
        };
        Runnable customerRunable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <Integer.MAX_VALUE; i++) {
                    customer .getValue();
                }
            }
        };
        Thread t1 = new Thread(producerRunable);
        Thread t2 = new Thread(customerRunable);
        t1.start();
        t2.start();
    }

出现了错误:


两个线程都是wating,没有达到线程之间通信(加锁,释放锁)的目的。

这个写法,用语法层面synchronized是没错误的。

解决办法:

生产者和消费者使用同一把锁,即同一类中。

public class ProducerAndCustomer {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void setValue() {
        try {
            lock.lock();
            if (!Value.value.equalsIgnoreCase("")) {
                condition.await();
            }
            String value = System.currentTimeMillis() + "_" + System.nanoTime();
            Value.value = value;
            System.out.println(Thread.currentThread().getName()+"生产了value为:" + value);
            condition.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        synchronized ()
    }
    public void getValue() {
        try {
            lock.lock();
            if (Value.value.equalsIgnoreCase("")){
                condition.await();}
            System.out.println(Thread.currentThread().getName()+"消费了value,为:" + Value.value);
            Value.value = "";
            condition.signal();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}

测试方法:

public class TestMain {
    public static void main(String[] args) throws Exception {
        ProducerAndCustomer producerAndCustomer = new ProducerAndCustomer ();
        Runnable producerRunable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <Integer.MAX_VALUE; i++) {
                    producerAndCustomer .setValue();
                    
                }
            }
        };
        Runnable customerRunable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <Integer.MAX_VALUE; i++) {
                    producerAndCustomer .getValue();

                }
            }
        };
        Thread t1 = new Thread(producerRunable);
        Thread t2 = new Thread(customerRunable);
        t1.start();
        t2.start();
    }
}

问题原因:没有使用同一个Lock,生产者和消费者都是使用了各自的Lock。所以释放锁和加锁,各自不影响,导致程序阻塞。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值