JDK and contracting refresher series (five) - Explicit lock explicit condition

Explicit lock -Lock with ReadWriteLock
the JDK for the main achievement Lock is ReentrantLock, ReadWriteLock implementation is ReentrantReadWriteLock. This paper describes ReentrantLock.

ReentrantReadWriteLock
two locks a shared queue, two lock state variable represented by one of the atoms, the specific logic acquire and release locks.

The basic principle of ReentrantReadWriteLock:
read lock acquisition, requires only a write lock is not held by the thread can get, check the queue, waiting to be read one by one wake lock thread encountered waiting for a write lock thread stops
releasing the read lock, release the check whether the write lock and the read lock is held, if you have not been held awakened the next waiting thread.
write lock acquisition, only read-write locks are being held will not obtain a write lock.
Write release the lock, wake up the next thread waiting queue.
ReentrantLock
main method
void lock (); acquiring the lock, blocking, does not respond to interrupt, but will be recorded interrupt flag.
void lockInterruptibly () throws InterruptedException; acquire the lock in response to interrupt
boolean tryLock (); acquiring the lock, not blocking, real return, which normally takes the cycle call
throws InterruptedException boolean tryLock (long time, TimeUnit unit); blocked acquire locks in the time time of in response to interrupt
void unlock (); release the lock
condition newCondition (); New explicit conditions
Note: the interrupt response here means that if they are other thread interrupts (call interrupt method) throws InterruptedException exception.

Principle behind the
dependent method CAS reentrant atom count variable is realized with use.
Dependent LockSupport Methods:
public static void park (): give up execution right CPU, CPU is not scheduling, in response to the interrupt, when an interrupt occurs, park returns, the thread interrupt status is provided, additionally park is also possible unprovoked return, it is generally required to wait for the cycle to check the condition of the park meets. .
public static void parkNanos (long nanos) : the right to give up the CPU to perform in the nanos nanoseconds
public static void parkUntil (long deadline) : give up executive powers until deadline time (from 1970 milliseconds).
public static void unpark (Thread thread) : resume the thread, allowed to compete for the right to perform CPU.
A basis the AQS
the AQS-AbstractQueuedSynchronizer (abstract queue synchronizer).

ReadWriteLock AbstractQueuedSynchronizer injected inside, locking and releasing the lock core which methods are based AQS, AQS core maintains two variables, is a State (reentrant current count initial value is 0), a is exclusiveOwnerThread (holding current lock thread thread object). It also maintains a lock wait queue.

ReentrantLock constructor passed a boolean ture fair locks, false lock is unfair. Unfair lock, for example speak about the principle of locking and releasing locks:

Lock
if the current lock status of 0 (not locked), CAS is used to obtain the lock, and lock in the current set of threads for themselves.
If not 0, and the thread holding the lock is not their own, then added to the end of the queue, and calls LockSupport in the park () method to give up executive power CPU. When the lock is released until the time of wake up, wake up after the first check whether he is a waiting thread, if it is and can get the lock, return, or continue to wait for this process if the interruption occurs, lock will break record flag, but does not return or throw an exception in advance.
If not zero, but the thread that holds the lock of their own, then add directly to the state 1.
Release of the lock
is to decrement the value of the state variable in the AQS 1, if the state value is 0, the lock is released completely, will "locking threads" variable is also set to null, while waiting queue wakeup first thread.

Fair Lock
why the above is not fair locks, not wake up in the queue when the first thread releases the lock it? Why would there have been an unfair situation, because if just release the lock, then there is a thread tries to acquire locks come in, there may be a queue jumping.

Fair lock principle
constructor bollean passing true representative of the fair locks, one more check method to acquire the lock, the other meaning is only waiting longer thread does not exist, it will try to acquire the lock. Lock unfair comparison, its overall performance is relatively low, a low not because this check is slow, but will not be active thread lock, enter the wait state, causing a context switch, reducing the overall efficiency,

And synchrnized difference
tryLock can avoid deadlock caused by an infinite wait
has information on various methods of acquiring the lock API
can respond to interrupts
may limit
recommendations: synchronized previous less efficient than an explicit lock, but almost no difference between the two versions of the current efficiency, it is proposed that can be synchronized to a synchronized, the need to achieve synchronized impossible demands such as when the above difference, then consider ReentrantLock.

Display Condition
What is a condition
and wait and notify correspond, for thread cooperation, create a display condition corresponding to the display through the lock of Lock Condition newCondition () method;

Method
The main method is the await () and signal (), await () corresponding to the Object wait (), signal () corresponding to notify, signalAll () corresponding to notifyAll ()

用法示例
public class WaitThread extends Thread {
private volatile boolean fire = false;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();

@Override
public void run() {
try {
lock.lock();
try {
while (!fire) {
condition.await();
}
} finally {
lock.unlock();
}
System.out.println("fired");
} catch (InterruptedException e) {
Thread.interrupted();
}
}

Fire void public () {
Lock.lock ();
the try {
this.fire = to true;
condition.signal ();
} {the finally
lock.unlock (with explicit lock -Lock ReadWriteLock
the JDK is achieved for the primary ReentrantLock of Lock, ReadWriteLock implementation is ReentrantReadWriteLock. This paper describes ReentrantLock.

ReentrantReadWriteLock
two locks a shared queue, two lock state variable represented by one of the atoms, the specific logic acquire and release locks.

The basic principle of ReentrantReadWriteLock:
read lock acquisition, requires only a write lock is not held by the thread can get, check the queue, waiting to be read one by one wake lock thread encountered waiting for a write lock thread stops
releasing the read lock, release the check whether the write lock and the read lock is held, if you have not been held awakened the next waiting thread.
write lock acquisition, only read-write locks are being held will not obtain a write lock.
Write release the lock, wake up the next thread waiting queue.
ReentrantLock
main method
void lock (); acquiring the lock, blocking, does not respond to interrupt, but will be recorded interrupt flag.
void lockInterruptibly () throws InterruptedException; acquire the lock in response to interrupt
boolean tryLock (); acquiring the lock, not blocking, real return, which normally takes the cycle call
throws InterruptedException boolean tryLock (long time, TimeUnit unit); blocked acquire locks in the time time of in response to interrupt
void unlock (); release the lock
condition newCondition (); New explicit conditions
Note: the interrupt response here means that if they are other thread interrupts (call interrupt method) throws InterruptedException exception.

Principle behind the
dependent method CAS reentrant atom count variable is realized with use.
Dependent LockSupport Methods:
public static void park (): give up execution right CPU, CPU is not scheduling, in response to the interrupt, when an interrupt occurs, park returns, the thread interrupt status is provided, additionally park is also possible unprovoked return, it is generally required to wait for the cycle to check the condition of the park meets. .
public static void parkNanos (long nanos) : the right to give up the CPU to perform in the nanos nanoseconds
public static void parkUntil (long deadline) : give up executive powers until deadline time (from 1970 milliseconds).
public static void unpark (Thread thread) : resume the thread, allowed to compete for the right to perform CPU.
A basis the AQS
the AQS-AbstractQueuedSynchronizer (abstract queue synchronizer).

ReadWriteLock AbstractQueuedSynchronizer injected inside, locking and releasing the lock core which methods are based AQS, AQS core maintains two variables, is a State (reentrant current count initial value is 0), a is exclusiveOwnerThread (holding current lock thread thread object). It also maintains a lock wait queue.

ReentrantLock constructor passed a boolean ture fair locks, false lock is unfair. Unfair lock, for example speak about the principle of locking and releasing locks:

Lock
if the current lock status of 0 (not locked), CAS is used to obtain the lock, and lock in the current set of threads for themselves.
If not 0, and the thread holding the lock is not their own, then added to the end of the queue, and calls LockSupport in the park () method to give up executive power CPU. When the lock is released until the time of wake up, wake up after the first check whether he is a waiting thread, if it is and can get the lock, return, or continue to wait for this process if the interruption occurs, lock will break record flag, but does not return or throw an exception in advance.
If not zero, but the thread that holds the lock of their own, then add directly to the state 1.
Release of the lock
is to decrement the value of the state variable in the AQS 1, if the state value is 0, the lock is released completely, will "locking threads" variable is also set to null, while waiting queue wakeup first thread.

Fair Lock
why the above is not fair locks, not wake up in the queue when the first thread releases the lock it? Why would there have been an unfair situation, because if just release the lock, then there is a thread tries to acquire locks come in, there may be a queue jumping.

Fair lock principle
constructor bollean passing true representative of the fair locks, one more check method to acquire the lock, the other meaning is only waiting longer thread does not exist, it will try to acquire the lock. Lock unfair comparison, its overall performance is relatively low, a low not because this check is slow, but will not be active thread lock, enter the wait state, causing a context switch, reducing the overall efficiency,

And synchrnized difference
tryLock can avoid deadlock caused by an infinite wait
has information on various methods of acquiring the lock API
can respond to interrupts
may limit
recommendations: synchronized previous less efficient than an explicit lock, but almost no difference between the two versions of the current efficiency, it is proposed that can be synchronized to a synchronized, the need to achieve synchronized impossible demands such as when the above difference, then consider ReentrantLock.

Display Condition
What is a condition
and wait and notify correspond, for thread cooperation, create a display condition corresponding to the display through the lock of Lock Condition newCondition () method;

Method
The main method is the await () and signal (), await () corresponding to the Object wait (), signal () corresponding to notify, signalAll () corresponding to notifyAll ()

用法示例
public class WaitThread extends Thread {
private volatile boolean fire = false;
private Lock lock = new ReentrantLock(www.yuanyangyuL.com);
private Condition condition = lock.newCondition();

@Override
public void run() {
try {
lock.lock();
try {
while (!fire) {
condition.await();
}
} finally {
lock.unlock();
}
System.out.println("fired");
} catch (InterruptedException e) {
Thread.interrupted(www.osgjyl.com );
}
}

public void fire() {
lock.lock();
try {
this.fire = true;
condition.signal();
} finally {
lock.unlock();
}
}

static void main public (String [] args) {throws InterruptedException
WaitThread waitThread new new WaitThread = ();
waitThread.start (www.qjljdgt.cn);
the Thread.sleep (1000);
System.out.println ( "Fire");
waitThread.fire (www.xinhuihpw.com);
}
}
when the main thread calls the fire method, sub-thread continues woken up. );
}
}

static void main public (String [] args) {throws InterruptedException
WaitThread waitThread new new WaitThread = ();
waitThread.start (www.rhyl158.com;
the Thread.sleep (1000);
System.out.println ( "Fire");
waitThread .fire ();
}
}
when the main thread calls fire method, the child thread woken continue.

Guess you like

Origin www.cnblogs.com/dakunqq/p/11621322.html
Recommended