Java Lock's re-entry lock (Reentrantlock) principle, fair lock and non-fair lock

Source: Internet
Author: User
Tags cas

1. Features:

The thread that has acquired the lock requests the lock again, which can be obtained directly.

2. Realize:

Custom internal class Sync, inheriting Abstarctqueuedsynchronizer:

2.1. Get lock : Lock ()

A, Fair lock:

Acquire (1)

B, non-fair lock:

if (compareandsetstate (0, 1))

CAS, the current state is 0, the current thread holds the lock:
Setexclusiveownerthread (Thread.CurrentThread ());
else acquire (1);

B.1 Abstarctqueuedsynchronizer Acquire (int acquires)

Then acquire (1)--Call the parent class Abstarctqueuedsynchronizer acquire (int acquires)

Public final void acquire (int arg) {
if (!tryacquire (ARG) &&
Acquirequeued (Addwaiter (node.exclusive), Arg))
Selfinterrupt ();
}

That is, if the lock is not acquired, the request lock thread joins the wait-two queue tail. The interrupt command is then issued to the current thread.

Node.exclusive = NULL,

Addwaiter (node node) joins NULL in the wait queue trailer, bidirectional queue.

Acquirequeued () Time again Tryacquire (ARG)

B.2 reetrantlock tryacquire (int acquires)

Reetrantlock inherits from Abstarctqueuedsynchronizer's Tryacquire (int acquires) as follows (using < template mode >):

Determines whether the current thread can acquire a lock based on the current state, Tryacquire (int acquires):

If no thread holds the current lock: if 0 = = GetState ():

Fair lock : CAs sets state to the current number of occurrences, then wakes the first thread in the queue to occupy the lock.

Non-fair lock : CAs sets state to the current number of times that the current thread occupies the lock directly.

If the current thread holds a lock: else if present = = Getexclusiveownerthread (), its hold status value is added to Aquires.

2.2. Release lock : Unlock ()-->abstarctqueuedsynchronizer release (1)--sync.tryrelease (1)

State = GetState-1

If the current thread is not holding a lock thread, throw an exception;

state = = 0, currently holding the lock thread is empty;

Otherwise, setState (state).

Java Lock's re-entry lock (Reentrantlock) principle, fair lock and non-fair lock

Related Article

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.