Introduction to the reentrant lock of Java lock _java

Source: Internet
Author: User

Locks are a tool for concurrent shared data, ensuring consistency, and many implementations (such as synchronized and Reentrantlock, etc.) on the Java platform. These have been written to provide the lock to facilitate our development, but the specific nature of the lock and the type is rarely mentioned. This series of articles will analyze the common lock names and features in Java to answer questions.

Four, can be reset lock:

This article is about a generalized reentrant lock, not a single reentrantlock under Java.

A reentrant lock, also known as a recursive lock, refers to the code in which the inner recursive function acquires the lock, but is unaffected, after the same thread's outer function acquires the lock.
Reentrantlock and synchronized are reentrant locks in the Java environment.

The following are examples of usages:

Copy Code code as follows:

public class Test implements runnable{

Public synchronized void get () {
System.out.println (Thread.CurrentThread (). GetId ());
Set ();
}

Public synchronized void set () {
System.out.println (Thread.CurrentThread (). GetId ());
}

@Override
public void Run () {
Get ();
}
public static void Main (string[] args) {
Test ss=new test ();
New Thread (ss). Start ();
New Thread (ss). Start ();
New Thread (ss). Start ();
}
}

Two examples of the final results are correct, that is, the same thread ID was continuously exported two times.

The results are as follows:

Copy Code code as follows:

Threadid:8
Threadid:8
Threadid:10
Threadid:10
Threadid:9
Threadid:9

The most important function of a reentrant lock is to avoid deadlocks.
We use spin lock as an example.

Copy Code code as follows:

public class Spinlock {
Private atomicreference<thread> owner =new atomicreference<> ();
public void Lock () {
Thread current = Thread.CurrentThread ();
while (!owner.compareandset (null, current)) {
}
}
public void unlock () {
Thread current = Thread.CurrentThread ();
Owner.compareandset (current, null);
}
}

For spin locks:

1, if there is the same thread two call lock (), will cause the second call lock position to spin, resulting in a deadlock
Indicates that the lock is not reentrant. (Within the lock function, you should verify that the thread is the thread that has acquired the lock)
2, if the 1 problem has been resolved, when unlock () The first call, the lock has been released. Locks should not actually be released.
(Counting by Count)

After modification, the following:

Copy Code code as follows:

public class SpinLock1 {
Private atomicreference<thread> owner =new atomicreference<> ();
private int count = 0;
public void Lock () {
Thread current = Thread.CurrentThread ();
if (Current==owner.get ()) {
count++;
return;
}

while (!owner.compareandset (null, current)) {

}
}
public void unlock () {
Thread current = Thread.CurrentThread ();
if (Current==owner.get ()) {
if (count!=0) {
count--;
}else{
Owner.compareandset (current, null);
}

}

}
}

The spin lock is a reentrant 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.