DirectoryWhat is a reentrant lock and why do I need to be able to reentrant the lock with a reentrant lock? Demo Code Show reference article
1. What is a reentrant lock
The concept of a lock is not to be explained more, when a thread A is already holding a lock, a thread B attempts to enter the code snippet protected by this lock. It will be blocked. The operation granularity of the lock is "thread", not call (as for why so, explained below). When the same thread enters the synchronization code again. You can use the locks that you have acquired, which is the reentrant lock
Inside Java, built-in locks (synchronize) and lock (Reentrantlock) are reentrant 2. Why do I have to be reentrant
What if thread a continues to get this lock again? For example, a method is synchronized, recursive call itself, then the first time has been a lock, the second call when you can enter? Intuitively, of course, you need to be able to enter. This requires that it be reentrant. A reentrant lock is also called a recursive lock, for example.
1
2
3
4 5 6 7 8
9
ten
-one 12
|
public class Widget {public
synchronized void dosomething () {
...}}
}
public class Loggingwidget extends Widget {public
synchronized void dosomething () {
System.out.println ( ToString () + ": Calling dosomething");
Super.dosomething ()//If the built-in lock is not reentrant, a deadlock
}
}
|
This example is a real example of Java concurrency programming. Synchronized is a built-in lock for the parent widget, which, when executing the subclass's method, first acquires a widget lock, and then, when performing super, takes one, and if not, kneels. 3. How to implement a reentrant lock
A fetch counter and an owner thread are associated for each lock, and when the count is 0, this one is not only by any thread. When a thread requests an unlocked lock, the JVM notes the lock holder, and the fetch count is set to 1, and if the same thread acquires the lock again, the technical value is incremented. Exits a synchronized code block, the computed value decrements, and when the count is 0 o'clock, the lock is released.
Reentrantlock inside has the realization 4. Is there a lock that can't be reset?
This is really. The pthread_mutex_t lock under Linux is not recursive by default. You can set the pthread_mutex_t lock as a recursive lock by setting the Pthread_mutex_recursive property. If you want to implement the non-reentrant lock, the same can be reset to the lock, this counter can only be 1, or 0, when the second entry, the discovery is already 1, will be blocked. There is no default implementation class in the JDK. 5. Demo Code Show
5.1 Built-in locks can be reentrant
1
2
3
4
5
6
7 8 9
18
|
public class Reentranttest {public
void Method1 () {
synchronized (reentranttest.class) {
System.out.println ("Method 1 gets the reentranttest's built-in lock to run");
Method2 ();
}
The public void Method2 () {
synchronized (reentranttest.class) {
System.out.println (Method 1, which is called in Method 2, is back in the built-in lock and is running correctly.) );
}
}
public static void Main (string[] args) {
new Reentranttest (). METHOD1 ();
}
}
|
5.2 Lock object's reentrant
1
2
3
4
5
6
7 8 9 (
29) (a)
|
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;
public class Reentrantlocktest {
private lock lock = new Reentrantlock ();
public void Method1 () {
lock.lock ();
try {
System.out.println ("Method 1 obtains Reentrantlock lock Run");
Method2 ();
} finally {
lock.unlock ();
}
}
public void Method2 () {
lock.lock ();
try {
System.out.println (method 2 called in Method 1 Reentrantlock lock, also normal);
} finally {
lock.unlock ()
}} Public
static void Main (string[] args) {
new Reentrantlocktest (). METHOD1 ();
}
}
|
5.3 Different threads cannot access the same lock
1 2 3 4 5 6 7 8 + 9, at the |
import java.util.concurrent.locks.Lock; import Java.util.concurrent.locks.ReentrantLock;
public class Reentranttwothreadtest {private static lock lock = new Reentrantlock (); private static class T1 extends thread {@Override public void run () {System.out.println ("Thread 1
Start ");
Lock.lock ();
try {thread.sleep (10000);
catch (Interruptedexception e) {e.printstacktrace ();
finally {Lock.unlock ();
} System.out.println ("Thread 1 terminated"); }} private static class T2 extends Thread {@Override public void run () {System.out
. println ("Thread 2 Starts");
Lock.lock ();
Lock.unlock ();
SYSTEM.OUT.PRINTLN ("Thread 2 terminated");
} public static void Main (string[] args) {new T1 (). Start ();
Thread.Sleep (100);
New T2 (). Start (); }
}
|
6. Reference ArticlesA more authentic example of a re-lock test producer Consumer talking about the lock Java concurrency programming in Java