Reentrantlock-mutex Synchronization

Source: Internet
Author: User

Reentrantlock is a mutually exclusive synchronization machine that implements the interface lock. Its functions include:
1. Lock () -- gets resources in blocking mode
2. lockinterruptibly () -- Obtains resources in the interrupted mode.
3. trylock () -- try to get the resource
4. trylock (time) -- try to get resources within a period of time
5. Unlock () -- release resources

Reentrantlock implements lock in two modes: fair mode and unfair mode.
The synchronizers in the concurrent package are all based on the AQS framework. The three classes are displayed in reentrantlock.
-----------------------------------------------------------------------
Static abstract class sync extends actqueuedsynchronizer {
Abstract void lock ();
Final Boolean nonfairtryacquire (INT acquires ){...}
Protected final Boolean tryrelease (INT releases ){...}
}
-----------------------------------------------------------------------
Final Static class nonfairsync extends sync {
Protected final Boolean tryacquire (INT acquires ){...}
Final void lock (){...}
}
-----------------------------------------------------------------------
Final Static class fairsync extends sync {
Final void lock (){...}
Protected final Boolean tryacquire (INT acquires ){...}
}
-----------------------------------------------------------------------
Then return to the lock Implementation of reentrantlock.
0. reentrantlock instantiation
Reentrantlock has an attribute sync. In fact, the implementation of the lock interface encapsulates the implementation of this sync.
If it is in fair mode, a fairsync object is created. Otherwise, a nonfairsync object is created. The default mode is unfair.
1. Lock () call sync. Lock ()
In fair mode: directly use the acquire function of AQS. The logic of this function goes through tryacquire once.
The thread requests the control of the synchronization machine. Otherwise, the node linked list is added to access the tryacquire of acquirequeued, sleep, and awakened cycle.
The logic in unfair mode is basically the same as that in fair mode. There are two differences:
A. the operation before tryacquire is executed. In unfair mode, the resource of AQS is set to compareandsetstate (0, 1) atomicity.
0 indicates that no threads occupy resources, and resources are directly preemptible regardless of the FIFO principle of the AQS node linked list.
B. tryacquire works differently. The Unfair tryacquire mode only checks whether compareandsetstate (0, 1) is successful.
In the fair mode, a condition is added that the node in this thread is not the first node linked list.
C. The implementation of tryacquire is different, while the logic of fair mode and unfair mode is the same During lock (the logic of AQS's acquirequeued)
D. Calling lock after a thread obtains the resource will accumulate AQS resources. Similarly, the thread must completely release the resource.
Number of calls to unlock for the corresponding subtraction operation, because for reentrantlock, a required condition for tryacquire to succeed is compareandsetstate (0, 1)
E. Because the thread interruption is blocked in the acquirequeued process, the thread is marked if the recorded thread is interrupted during this period after the thread requests the synchronization control.
Interruption status
2. lockinterruptibly () calls sync. acquireinterruptibly (1), previous Article We have talked about the core function of AQS. This process is related to acquirequeued.
It is the same, except that if the thread is marked to be interrupted during the blocking period, it will be awakened during the park period, and then exit the loop directly, throwing an interrupt exception.
The lockinterruptibly logic is also different because the implementation of tryacquire is different in the fair mode and unfair mode.
3. The trylock () function only tries to obtain the lock. Like tryacquire Code Code in fair mode.
4. trylock (time) calls sync. tryacquirenanos (time). In the previous article, we talked about AQS's core functions. This process is the same as acquirequeued,
A. Before blocking, the system calculates the blocking time and goes to sleep.
B. If it is interrupted, it will determine whether the time has arrived.
1. If the thread does not arrive and the interrupt mark is set by other threads, exit the loop and throw an interrupt exception. If the thread is not set with the interrupt mark, it is the previous thread.
Release the resource and then wake it up. It continues to go through the cycle. In the cycle, if tryacquire is successful, it will get rid of the control of the synchronization machine; otherwise, it will return to
2. If the time reaches, the system will exit the loop and fail to get the resource.
5. Unlock () calls sync. Release (1). In the previous article, we talked about the core function of AQS. The release function will call the tryrelease function implemented by sync to judge
Whether the resource is successfully released, that is, the sync. tryrelease function. The logical process is
A. First, determine whether the thread that currently occupies the resource is the caller. If not, an illegalmonitorstateexception will be thrown.
B. If yes, the logic of reducing AQS resources by 1 will be implemented. If AQS resources change to 0 after 1 is subtracted, the call thread will discard the lock and return true to the value of release,
Release will wake up the next thread
-----------------------------------------------------------------------
Overall, the implementation of the reentrantlock mutex lock is roughly
1. Implement the tryacquire and tryrelease logic of AQS by yourself. tryacquire indicates to try to get the lock, and tryrelease indicates to try to release the lock.
2. The implementation of reentrantlock on lock (), trylock (), trylock (time), and unlock () all use the AQS framework, and then the AQS framework returns a call.
Tryacquire and tryrelease implemented by reentrantlock are used to determine whether the thread obtains or releases the lock.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.