In the end what is reentrant lock, please, first find out!

I believe we often hear the concept of re-entry lock at work or in the interview process, or contrast with the keyword synchrozied stack long interview so many people, 80 percent of interviewees did not answer or no answer on the point, or the double lock efficacy confused, dumbfounding. .

So how much do you have to re-enter the lock understand it? Today, the stack length to help you tear reentrant lock veil, to experience the true face under reentrant lock. .

What is reentrant lock

java.util.concurrent.locks.ReentrantLock

This is a particle size JDK @since 1.5 added a smaller lock, it can replace the synchronized keyword to implement all of its features, and flexibility ReentrantLock lock is much larger than the synchronized keyword.

Seen from FIG class structure, ReentrantLock achieved Lock interfaces, ReentrantLock just one implementation Lock interface only.

java.util.concurrent.locks.Lock

They are java.util.concurrent package contents inside (commonly known as the JUC, and contract), they are also beginning to join the JDK 1.5.

Why is it called re-entry lock it?

ReentrantLock, we take it apart to see it clear.

Re-Entrant-Lock: means that can be repeatedly re-enter the lock, but only for the current thread;


public void m() {

lock.lock();

lock.lock();

try {

// ... method body

} finally {

lock.unlock()

lock.unlock()

}

}

As shown in the sample code, the current thread may be repeatedly locked, but needs to be released the same number of locking the lock, i.e., how many times re-entry, the number of times necessary to release, or will be introduced into the lock is not released.

Imagine, if not designed to be reentrant lock, that lock themselves if repeated himself, not put myself plus a deadlock yet? So, up to now, the concept of re-entry lock should probably clear now?

Reentrant lock a few of the most important methods

Lock these methods are defined in the interface:

1)lock()

Acquiring the lock, the following three conditions:

  • Lock Idle: direct access to the lock and returns, and set the number of lock holders: 1;

  • The current thread holds the lock: direct access to the lock and return, while the number of lock holder is incremented by 1;

  • Other thread holds the lock: the current thread to sleep to wait until the lock acquisition date;

2)lockInterruptibly()

As acquiring the lock, and logic lock () method, but this method in the process of acquiring the lock can respond to the interrupt.

3)tryLock()

Literally from the keyword, which is trying to acquire the lock, get successful return: true, failure to obtain returns: false, this method does not wait, the following three conditions:

  • Lock Idle: direct access to the lock and returns: true, and set the number of lock holders: 1;

  • The current thread holds the lock: direct access to the lock and returns: true, while the number of lock holder is incremented by 1;

  • Other thread holds the lock: to acquire the lock fails, returning: false;

4)tryLock(long timeout, TimeUnit unit)

TryLock logical sum () is almost, but this method is time band.

5)unlock()

Release the lock, the lock every time the number of holders decremented until 0. So, why the lock now know how many times, how many times will correspond to unlock it.

6) newCondition

Returned Condition instance of this lock, you can achieve a similar wait / notify realize the function of multi-threaded communications synchronized keyword, but this should be more flexible than wait / notify, more powerful!

Reentrant lock rough usage


class X {

private final ReentrantLock lock = new ReentrantLock();

 

// ...

public void m() {

lock.lock(); // block until condition holds

try {

// ... method body

} finally {

lock.unlock()

}

}

 

}}

See no, locking and releasing locks are inside methods, can be freely controlled, more flexible than synchronized, more convenient. But it notes that the release lock operation must finally inside, or if there is an abnormal result in the lock can not be properly released, and then will get stuck all subsequent access to the lock thread.

synchronized is reentrant lock on it?

So the question is, synchronized reentrant lock on it?

You might say it is not, because since it is ReentrantLock reentrant lock, according to the reasoning, on the contrary, it is certainly not synchronized reentrant lock, then you are wrong.

The answer is: yes, Why? See the examples below:


public synchronized void operation(){

add();

}

public synchronized void add(){

}

operation method calls the add method, two methods are synchronized with the modified, add () method can successfully get the current thread operation () method has been to acquire a lock, indicating that synchronized is reentrant lock.

Interview Frequently Asked Synchronized recommend the use of several look this article: Synchronized there are several uses? .

to sum up

Today, the reentrant lock is probably write to you, in fact, is a kind of re-entry lock granularity smaller locks, control is more convenient, more powerful, longer stack simply explain the basic concepts and the use of re-entry lock, but well more than that simple, there are many, it is difficult to have a detailed enough to write a lot of articles.

We can also focus on micro-channel public number: Java technology stack, the stack length will continue to share more advanced concepts and work re-entry lock in actual combat use, please pay attention to the follow-up article, or in the public No. backstage Re: multithreading, stack length has collated a number of multi-threaded Java series, are grounded air dry.

I find it useful, under forwarded circle of friends to share the more people see, in addition, to a good-looking, thank you boss ~

Focus on Java technology stack micro-channel public number, the stack will continue to share a long dry Java tutorial, the first time to push public number, sustained attention. No background in public reply: java, Java tutorial for more long stacks finishing, dry goods are real, the only part of the preview.

  • You really get to know the transient keyword yet?

  • Interview often test: Synchronized there are several uses?

  • Java 11 has been released, String can this play!

  • In Java String is immutable really do?

  • sleep () and wait () is the difference between five

  • ……

This article first appeared in the original micro-channel public number: Java technology stack (id: javastack), reproduced Please keep this information intact.

Guess you like

Origin www.cnblogs.com/javastack/p/11095333.html