zoukankan      html  css  js  c++  java
  • Java生产者和消费者

    synchronized

    This version is the fewest lines of code, largely because the synchronization mechanism used is built into the language and runtime. But the programmer has to remember to avoid a couple of common bugs: The wait() must be inside a while instead of an if, and notifyAll() must be used instead of notify() because there are two different logical conditions being awaited.
     
       public class SafeBox<V> {
         private V value;
    
         public synchronized V get() throws InterruptedException {
           while (value == null) {
             wait();
           }
           V result = value;
           value = null;
           notifyAll();
           return result;
         }
    
         public synchronized void set(V newValue) throws InterruptedException {
           while (value != null) {
             wait();
           }
           value = newValue;
           notifyAll();
         }
       }
    
     

    ReentrantLock

    This version is much more verbose than the synchronized version, and still suffers from the need for the programmer to remember to use while instead of if. However, one advantage is that we can introduce two separate Condition objects, which allows us to use signal() instead of signalAll(), which may be a performance benefit.
     
    
       public class SafeBox<V> {
         private final ReentrantLock lock = new ReentrantLock();
         private final Condition valuePresent = lock.newCondition();
         private final Condition valueAbsent = lock.newCondition();
         private V value;
    
         public V get() throws InterruptedException {
           lock.lock();
           try {
             while (value == null) {
               valuePresent.await();
             }
             V result = value;
             value = null;
             valueAbsent.signal();
             return result;
           } finally {
             lock.unlock();
           }
         }
    
         public void set(V newValue) throws InterruptedException {
           lock.lock();
           try {
             while (value != null) {
               valueAbsent.await();
             }
             value = newValue;
             valuePresent.signal();
           } finally {
             lock.unlock();
           }
         }
       }
    

     

  • 相关阅读:
    使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
    Mybatis学习 PageHelper分页插件
    mysql 5.1.7.17 zip安装 和 隔段时间服务不见了处理
    使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
    一位资深程序员大牛给予Java初学者的学习建议
    数据结构和算法学习 -- 线性表
    多线程的实现方式区别
    Log4j.properties属性文件
    Java自定义注解
    Spring配置属性文件
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/8807840.html
Copyright © 2011-2022 走看看