Deadlock in Java: when occurs

I'm developing an application for J2ME. Sometimes it's completely frozen. AMS takes a long time to close it In my opinion, it's like a deadlock problem

Can you tell me that it may cause deadlock? For example, if an object's synchronization method calls another object's own synchronization method, will it cause a deadlock?

thank you!

to update

I am right to say that an impasse should occur under the following circumstances:

Object P calls the synchronization method of object a, which calls the synchronization method of object B, which calls the synchronization method of object a

Sorry, if it looks stupid to me, it may be But that's why I asked thank you!

Solution

No, because synchronous locks in Java are reentrant: you can get the same lock from the same thread multiple times without problem

Deadlock occurs, for example, when thread a holds lock L and attempts to acquire lock m, and thread B holds lock m and attempts to acquire lock L. therefore, two threads are waiting for a lock held by another lock and cannot release their own lock This causes two threads to wait forever The situation may involve more than two lines

Deadlocks can be very difficult to detect, so a typical approach is to avoid careful design The easiest way to do this is to ensure that any thread that acquires multiple locks always acquires them in the same predefined global order For example If both threads a and B try to acquire lock l first in the above example, lock m and there will be no deadlock

Your problem may be caused by something else rather than a deadlock, such as an active lock (when a thread is not blocked, it still can't progress because it keeps trying an operation that always fails)

Updating: accessing locks from outside objects

Using java intrinsic lock (i.e. synchronization block), the underlying lock object itself is not visible in the code, only the object we lock consider

class MyClass {
  private Object object = new Object();

  public synchronized void synchronizedOnThis1() {
    ...
  }
  public void synchronizedOnThis2() {
    synchronized(this) {
      ...
    }
  }
  public void synchronizedOnPrivateObject() {
    synchronized(object) {
      ...
    }
  }
}

class ExternalParty {
  public void messUpLocks() {
    final MyClass myObject = new MyClass();
    synchronized(myObject) {
      Thread otherThread = new Thread() {
        public void run() {
            myObject.synchronizedOnThis1();
        }
      };
      otherThread.start();
      // do a lengthy calculation - this will block the other thread
    }
  }
}

The synchronizedonthis * method synchronizes on the instance containing MyClass; The synchronization of the two methods is equivalent However, it is obviously accessible outside the class, so an external party can use it as an external lock, as shown in the figure above And if the object can be accessed from another thread and the thread calls one of its synchronized onthis * methods, the call will be blocked as long as the thread is in the synchronized (MyObject) block

The Otoh method synchronizedonprivateobject uses a private object as a lock If the object is not published to an external party in any way, no one else will (unintentionally or maliciously) cause a deadlock involving this lock

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>