"Re-entry in Java lock-dead"

Source: Internet
Author: User

original link Jakob Jenkov Translator: Liu Xiaozhi proofreading: Ding

Re-entry locking is very similar to deadlock in Java. Lock and read-write lock both articles involve re-entry locking.

When a thread acquires a lock, a read-write lock, or another non-reentrant synchronizer, a re-entry lock can occur. Reentrant means that a thread can repeatedly acquire a lock that it already holds. Java's synchronized blocks are reentrant. So the following code is not a problem:

(Translator Note: The locks mentioned here refer to the non-reentrant lock implementations, not the lock and Readwritelock classes in the Java class Library)

  1. public class reentrant{
  2. Public synchronized outer () {
  3. Inner ();
  4. }
  5. Public synchronized inner () {
  6. Do something
  7. }
  8. }
Copy Code

Meaning outer () and inner () are declared as synchronized, which in Java is equivalent to the synchronized (this) block ( Translator Note: Here are two methods that are instance methods, The instance method of synchronized is equivalent to locking on this and, if it is a static method, otherwise, if a thread calls outer (), the inner () call in outer () is not a problem, since two methods are all in the same pipe object ( This is the synchronization. If a thread holds a lock on a pipe object, it has access to all the blocks that are synchronized on that pipe object. This is called re-entry. If a thread already holds a lock, it can repeatedly access all blocks of code that use that lock.

The following implementation of this lock is non-reentrant:

  1. public class lock{
  2. Private Boolean isLocked = false;
  3. Public synchronized void Lock ()
  4. Throws interruptedexception{
  5. while (isLocked) {
  6. Wait ();
  7. }
  8. IsLocked = true;
  9. }
  10. public synchronized void unlock () {
  11. isLocked = false;
  12. Notify ();
  13. }
  14. }
Copy Code

If a thread does not call the Unlock () method between two calls to lock (), then the second call to lock () is blocked, and a re-entry deadlock occurs.

To avoid re-entry locking there are two options:

    • Avoid acquiring locks that are already held once you write code
    • Use a reentrant lock

As to which choice is best for your project, it depends on the situation. Reentrant locks are generally not as good as non-reentrant locks and are complex to implement, but these situations may not be a problem in your project. Whether your project is easy to use with locks or locks, reentrant features need to be analyzed specifically for specific problems.

"Re-entry in Java lock-dead"

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.