简体   繁体   English

Java java.util.concurrent.locks.Condition接口文档

[英]Java java.util.concurrent.locks.Condition interface documentation

Inside the documentation for the Condition interface: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html 在Condition接口的文档中: http//docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html

there is an example class which uses the condition. 有一个使用条件的示例类。 I will copy/paste here: 我会在这里复制/粘贴:

class BoundedBuffer {
   final Lock lock = new ReentrantLock();
   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();
     }
   }
 }

What I do not understand is how this can possibly work (note: I did not test it, I just assume that it works) without creating a deadlock? 我不明白这是如何工作的(注意:我没有测试它,我只是假设它有效)而不会造成死锁?

If I call BoundedBuffer.put a hundred times then on the hundredth time it will be blocked in the notFull.await() method and this will be under a lock. 如果我调用BoundedBuffer.put一百次,那么在第一百次它将在notFull.await()方法中被阻塞,并且这将被锁定。 If I then do BoundedBuffer.take() from another thread then I should be blocked on the first line in the lock.lock(). 如果我然后从另一个线程做BoundedBuffer.take(),那么我应该在lock.lock()的第一行被阻塞。

Am I missing something here? 我在这里错过了什么吗?

Yes you do miss something. 是的,你错过了什么。 From the JavaDoc : 来自JavaDoc

The key property that waiting for a condition provides is that it atomically releases the associated lock and suspends the current thread, just like Object.wait. 等待条件提供的关键属性是它以原子方式释放关联的锁并挂起当前线程,就像Object.wait一样。

Hence, the lock is released when await() is called. 因此,在调用await()时释放锁。

From the JavaDoc of the await() method itself: await()方法本身的JavaDoc:

void await() throws InterruptedException void await()抛出InterruptedException

Causes the current thread to wait until it is signalled or interrupted. 导致当前线程等待,直到发出信号或中断为止。 The lock associated with this Condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens: 与此条件关联的锁被原子释放,并且当前线程因线程调度而被禁用,并处于休眠状态,直到发生以下四种情况之一:

And the method signal() awakes a thread which will re-acquire the lock before continuing. 并且方法signal()唤醒一个线程,该线程将在继续之前重新获取锁。 From the JavaDoc of the method signal() : 从方法signal()的JavaDoc:

void signal() 无效信号()

Wakes up one waiting thread. 唤醒一个等待线程。 If any threads are waiting on this condition then one is selected for waking up. 如果任何线程正在等待这种情况,则选择一个线程进行唤醒。 That thread must then re-acquire the lock before returning from await. 然后该线程必须在从await返回之前重新获取锁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.util.concurrent.locks.Condition awaitUninterruptible() - java.util.concurrent.locks.Condition awaitUninterruptibly() getState()返回java.util.concurrent.locks.Condition的值 - getState() return value for java.util.concurrent.locks.Condition 内存屏障和java.util.concurrent.locks.Condition示例 - Memory barrier and java.util.concurrent.locks.Condition example java.util.concurrent.locks.Condition如何工作? - How do java.util.concurrent.locks.Condition work? java.util.concurrent.locks.ReentrantReadWriteLock的文档 - Documentation for java.util.concurrent.locks.ReentrantReadWriteLock 我可以将java.util.concurrent.locks.Lock更改为java.util.concurrent.locks.ReentrantReadWriteLock; - Can i change java.util.concurrent.locks.Lock into java.util.concurrent.locks.ReentrantReadWriteLock; java.util.concurrent.locks.Lock的AutoCloseable包装中的任何风险? - Any risk in a AutoCloseable wrapper for java.util.concurrent.locks.Lock? 有没有一种方法可以从java.util.concurrent.locks.ReentrantReadWriteLock $ ReadLock获取java.util.concurrent.locks.ReentrantReadWriteLock对象 - Is there a way i can get the java.util.concurrent.locks.ReentrantReadWriteLock object from java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock 是否存在与java.util.concurrent.locks.ReentrantReadWriteLock等效的C ++? - Is there a C++ equivalent of java.util.concurrent.locks.ReentrantReadWriteLock? 带有java.util.concurrent.locks.ReadWriteLock的@GuardedBy注释 - @GuardedBy annotation with java.util.concurrent.locks.ReadWriteLock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM