java.util.concurrent.locks.Condition如何工作? [英] How do java.util.concurrent.locks.Condition work?

查看:160
本文介绍了java.util.concurrent.locks.Condition如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读有关java.util.concurrent.locks.Condition接口的Java 8文档,给出以下示例:

Reading the Java 8 documentation about the java.util.concurrent.locks.Condition interface, the following example is given:

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();
     }
   }
 }

所以我的主要问题是:条件如何起作用?

So my main question is: How does a condition work?

  1. 当它开始等待时,它会释放锁吗? (例如notFull.await())?
  2. 不同的线程能否获得相同的锁并移动直到它成为条件,从而唤醒其他线程?
  3. 我认为该示例将导致死锁,因为如果线程正在等待缓冲区不为空并且它还没有释放锁,那么另一个线程如何获取该锁,清空缓冲区,signal()现在满足条件,并在等待缓冲区未满的线程未释放锁的情况下释放锁?
  1. Does it release the lock when it starts waiting? (notFull.await() for example)?
  2. Can different threads acquire the same lock and move until it signal() a condition, awaking other threads?
  3. I thought this example would result in a deadlock because if a thread is waiting for the buffer to not be empty and it hasn't release the lock yet, how can another thread acquire the lock, empty the buffer, signal() the condition is now met, and release the lock if the lock hasn't been release by the thread waiting for the buffer to be not full?

这些是初学者的问题.请帮帮我.

These are beginners questions. Please help me out.

谢谢.

推荐答案

  1. 是的,一旦等待条件,它将释放锁. await/signal/signalAll实际上具有与wait/notify/notifyAll
  2. 相同的行为
  3. 没有ReentrantLock是排他锁,因此只有一个线程可以获取该锁.
  4. 请参阅#1.当线程调用signalsignalAll时,它将分别释放一个线程或所有等待相应Condition的线程,以便该一个或多个线程将有资格再次获取该锁.但是现在,该锁仍由调用signalsignalAll的线程拥有,直到它通过调用lock.unlock显式释放该锁为止.然后已经/已经释放的线程将能够再次尝试获取锁,可以获取锁的线程将能够再次检查条件(通过这次的条件,我的意思是count == items.lengthcount == 0),如果可以,将继续执行,否则将再次await并释放该锁以使其可用于另一个线程.
  1. Yes that is right, as soon as it awaits for a condition, it releases the lock. await/signal/signalAll has actually the same behavior as wait/notify/notifyAll
  2. No the ReentrantLock is an exclusive lock so only one thread can acquire the lock.
  3. See #1. When a thread call signal or signalAll, it releases respectively one thread or all threads awaiting for the corresponding Condition such that the thread or those threads will be eligible to acquire the lock again. But for now the lock is still owned by the thread that called signal or signalAll until it releases explicitly the lock by calling lock.unlock. Then the thread(s) that has/have been released will be able to try to acquire the lock again, the thread that could acquire the lock will be able to check the condition again (by condition this time I mean count == items.length or count == 0 in this example), if it is ok it will proceed otherwise it will await again and release the lock to make it available to another thread.

这篇关于java.util.concurrent.locks.Condition如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆