zoukankan      html  css  js  c++  java
  • java.util.concurrent.locks.Condition 例子程序探讨

    API文档上例子如下:
    class BoundedBuffer {
       final Lock lock = new ReentrantLock();
    下面使用两个condition是否有必要?
       final Condition notFull  = lock.newCondition();
       final Condition notEmpty = lock.newCondition();

       final Object[] items = new Object[100];
       int putptr, takeptr, count;

       public void put(Object x) throws InterruptedException {
         lock.lock();
         try {
           while (count == items.length)
             notFull.await();
           items[putptr] = x;
           if (++putptr == items.length) putptr = 0;
           ++count;
           notEmpty.signal();
         } finally {
           lock.unlock();
         }
       }

       public Object take() throws InterruptedException {
         lock.lock();
         try {
           while (count == 0)
             notEmpty.await();
           Object x = items[takeptr];
           if (++takeptr == items.length) takeptr = 0;
           --count;
           notFull.signal();
           return x;
         } finally {
           lock.unlock();
         }
       }
    }
  • 相关阅读:
    AVPlayer中的问题
    封装网络请求
    FMDB的使用方法
    设置UITextField占位符的颜色和字体
    SQL SERVER性能优化综述
    关于学习
    学习java中对《类与对象》的认知
    Felling1-java
    关于学习JAVA第二章的心得
    学习JAVA第一章的心得
  • 原文地址:https://www.cnblogs.com/end/p/2738448.html
Copyright © 2011-2022 走看看