Re-locking is a similar scenario for deadlocks and nested monitoring programs. The re-entry lock is also mentioned in the read and write lock.
Reentrant locking may occur if a thread is reentrant in a lock, readwritelock or some other Synchronizer is not reentrant. Reentrant means that a thread that already holds a lock can hold it again. The Java synchronization block is reentrant, so the following code will have no problem working:
public class reentrant{public
synchronized outer () {
inner ();
}
Public synchronized inner () {
//do something
}
}
Notice how this outer method and the inner method are declared as synchronous methods, this is the same as the synchronized (this) block in Java. If a thread calls the outer method and then calls the inner method inside the outer method, there is no problem because the two methods (or blocks) are synchronized on the same monitoring object ("this"). If a thread already holds a lock on a monitored object, it can access all the synchronized locks on the same monitored object. This is called re-entry. This thread can reload any block of code that it already holds a lock on.
The following implementation of this lock is not reentrant:
public class lock{
private Boolean islocked = false;
Public synchronized void Lock ()
throws interruptedexception{while
(islocked) {wait
();
}
IsLocked = true;
}
public synchronized void Unlock () {
islocked = false;
Notify ();
}
If a thread calls the lock method two times, the Unlock method is not invoked during the period, and the second call to the lock method is locked. A reentrant lock can happen.
You have two options for a reentrant lock:
Avoid writing code that resets the lock. Use a reentrant lock.
These choices are specific to what fits your specific scene. Re-entry locks are often not performed as a non-reentrant lock, and they are difficult to implement, but this may not be a problem in your scenario. Your code is simple to implement the lock or not reentrant, the specific situation of specific analysis.
Translation Address: http://tutorials.jenkov.com/java-concurrency/reentrance-lockout.html