Condition analysis

 

  Condition provides a set of monitor methods similar to Object. In conjunction with Lock, the waiting for notification mode can be completed.

  

  Condition can only be obtained through the Lock#newCondition() method, so Condition depends on Lock, and before calling this method, the thread needs to obtain the lock first.

  At the same time, in a Lock, multiple Condition objects can be obtained.

 

  Main method :

  void await() throws InterruptedException : Make the current thread enter the waiting state until it is notified (signal/signalAll) or interrupted. The current thread returns from the await() method, including:

    Other threads call the signal() or signalAll() method of the condition, and the current thread is selected to wake up

    · Other threads call the interrupt() method to interrupt the current thread

    · If the current thread returns from the await() method, it indicates that the thread has acquired the lock corresponding to the Condition object.

  void awaitUninterruptibly(): The current thread enters a wait state until notified, but will not be interrupted accordingly.

  void awaitNanos(long nanosTimeOut) throws InterruptedException: The current thread enters the waiting state until it is notified, interrupted or timed out. The return value indicates the remaining time. If it is woken up before nanosTimeout nanoseconds, then the return value is the actual time taken by nanosTimeout. If the return value is 0 or a negative number, it can be considered that it has timed out.

  boolean awaitUntil(Date deadline) throws InterruptedException: The current thread enters a wait state until notified, interrupted, or at a certain time. If it is notified before the specified time, the method returns true, otherwise, the method returns false when the time is up.

  void signal(): Wakes up a thread waiting on a Coindition that must acquire the lock associated with the Condition before returning from the wait method.

  void signalAll(): Wakes up all threads waiting on the Condition, the thread that can return from the wait method must acquire the lock associated with the Condition.

 

  How to use :

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

 

  Condition is an interface. In concurrent packets, the main implementation is the inner class ConditionObject in AbstractQueuedSynchronizer.

  For the implementation of ConditionObject, refer to here

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325007979&siteId=291194637