Concurrent advanced knowledge: lock, condition, ReadWriteLock Interface

LOck Introduction

lock main targets are the main features is an extension of sychronize keywords:

  1. sychronize acquire monitor directly into the block lock state, or wait time runs out to return. trylock lock can return a result, non-blocking tasks required
  2. When the process of acquiring multiple locks, get the order and release the reverse order. lock the object does not require the application more flexible.
  3. lock can obtain many times, for example ReadWriteLock.

lock interface functions Introduction

  1. void lock () to acquire the lock if the lock is invalid, then enter the wait state, it can not be interrupted
  2. void lockInterruptibly () throws InterruptedException interrupt lock. After entering the wait state can be interrupted
  3. boolean tryLock () tries to acquire the lock. Invalid direct return
  4. boolean tryLock (long time, TimeUnit unit) throws InterruptedException attempt to acquire the lock, if invalid wait for a certain time.
  5. void unlock () releases the lock
  6. Condition newCondition () returns the state of the lock associated with the object.

condition Interface

condition excuse object is to achieve the object wait (), notify (), notifyAll (). The main difference is that a subclass can implement the corresponding function, and be rich. related functions

  1. void await () throws InterruptedException call waiting, waiting to enter the condition set.
  2. void awaitUninterruptibly () call waiting, waiting to enter the condition set, it can not be interrupted.
  3. long awaitNanos (long nanosTimeout) throws InterruptedException call waiting, waiting to enter the condition set when the time is past time will trigger an interrupt.
  4. boolean await(long time,TimeUnit unit) throws InterruptedException 功能同上
  5. boolean awaitUntil (Date deadline) throws InterruptedException functions similar to the
  6. void signal () to select the successor waiting condition, waiting to enter the lock state
  7. void signalAll () and all of the wait state. Note that condition, like wait, before the implementation of the relevant function, to get the lock. condition is acquired lock an object, wait to obtain a lock monitor object itself

Example usage:

 boolean aMethod(long timeout, TimeUnit unit) {
   long nanos = unit.toNanos(timeout);
   lock.lock();
   try {
     while (!conditionBeingWaitedFor()) {
       if (nanos <= 0L)
         return false;
       nanos = theCondition.awaitNanos(nanos);
     }
     // ...
   } finally {
     lock.unlock();
   }
 }
复制代码

ReadWriteLock Interface

It is in the extended lock function on the basis of the mutex, allowing multiple processes to read, when the writing process a few times, can greatly improve efficiency. Design issues, priorities, and strategies literacy problems reading and writing process.

  1. Lock readLock()
  2. Lock writeLock()

ReentrantLock lock子类

Achieve lock interface, you can achieve fair algorithm to block the process, waiting for a long time to get the process has a limited lock. fair algorithm causes the program to performance degradation, and of course hunger and waiting time was less affected. Recommended default non-fair lock.


usage

   private final ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() {
     lock.lock();  // block until condition holds
     try {
       // ... method body
     } finally {
       lock.unlock()
     }
   }
复制代码

related functions:

  1. public ReentrantLock () default non fairlock
  2. public ReentrantLock (boolean fair) what kind of lock on demand selection
  3. public void lock () to acquire the lock if you can not get a lock, then entering the wait
  4. public void lockInterruptibly () throws InterruptedException acquire the lock if you can not get a lock, then entering the wait can be interrupted
  5. public boolean tryLock () tries to acquire the lock, true acquiring the lock, false lock is not acquired
  6. public boolean tryLock (long timeout, TimeUnit unit) throws InterruptedException acquire lock in a given time.
  7. public void unlock () releases the lock
  8. public Condition newCondition () to create a condition object. When the lock with the lock conditon wait automatically released, in the form of FIFO waiting condition is activated, it is necessary to re-acquire lock after activation
  9. public int getHoldCount () returns the process to obtain the number lock for unlock function. This value can also be obtained before the lock, without re-lock
  10. public boolean isHeldByCurrentThread () if the current process has been
  11. public boolean isLocked()是否locked
  12. public final boolean isFair () whether fair
  13. protected Thread getOwner () return goods lock owner
  14. public final boolean hasQueuedThreads () protected Collection getQueuedThreads (); public final int getQueueLength () whether to wait, set of threads waiting, waiting for the number of threads. Note that the value is not necessarily accurate.
  15. public boolean hasWaiters(Condition condition);public int getWaitQueueLength(Condition condition);protected Collection getWaitingThreads(Condition condition)同上只是针对condition的
  16. public String toString()

ReentrantReadWriteLock类

It implements readerwriterlock interface

  1. Sequence is obtained: a, disorder, default, high efficiency. b, fair, priority call waiting a long time to process low performance.
  2. Can I reacquire lock: write lock can get a read lock, not vice versa
  3. Performance degradation lock: write lock can be turned into a read lock, not vice versa. Meaning as above
  4. Write lock can be interrupted
  5. condition support, write. Read lock no condition
  6. Instructions, some instructions only appropriate monitoring. Not a process of communication, blocking information. Entrantlock correlation function with the same, do not explain

Quote

tutorial

Official description library

Guess you like

Origin juejin.im/post/5d0f4d9bf265da1b91639ded