Can several threads hold a lock on the same monitor in Java?

☆樱花仙子☆ 提交于 2019-12-19 08:57:44

问题


Currently we are analyzing a tomcat thread dump. A single thread dump of all threads running at that same time on a tomcat contains the following lines:

...
"soldOutJmsConsumerContainer-1" prio=10 tid=0x00007f8409c14800 nid=0x231 in Object.wait() [0x00007f8403a9f000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at com.tc.object.RemoteObjectManagerImpl.waitUntilRunning(RemoteObjectManagerImpl.java:150)
    at com.tc.object.RemoteObjectManagerImpl.basicRetrieve(RemoteObjectManagerImpl.java:216)
    - locked <0x00007f847612c820> (a com.tc.object.RemoteObjectManagerImpl)
...
"catalina-exec-33" daemon prio=10 tid=0x0000000041bc4000 nid=0x832 in Object.wait() [0x00007f8400f73000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at com.tc.object.RemoteObjectManagerImpl.waitUntilRunning(RemoteObjectManagerImpl.java:150)
    at com.tc.object.RemoteObjectManagerImpl.basicRetrieve(RemoteObjectManagerImpl.java:216)
    - locked <0x00007f847612c820> (a com.tc.object.RemoteObjectManagerImpl)
...
"catalina-exec-109" daemon prio=10 tid=0x0000000041469800 nid=0x1e87 in Object.wait() [0x00007f83f84c1000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at com.tc.object.RemoteObjectManagerImpl.waitUntilRunning(RemoteObjectManagerImpl.java:150)
    at com.tc.object.RemoteObjectManagerImpl.basicRetrieve(RemoteObjectManagerImpl.java:216)
    - locked <0x00007f847612c820> (a com.tc.object.RemoteObjectManagerImpl)

In particular we do not understand

- locked <0x00007f847612c820> (a com.tc.object.RemoteObjectManagerImpl)

In our understanding it says three threads are holding a lock to the same monitor at that time. In our understanding and according to JLS this is not possible.

Is our interpretation of the thread dump correct?


回答1:


It looks like all these threads are waiting for condition associated with the monitor, i.e. they called wait() method of that monitor.

When thread calls wait() on the monitor it owns, it temporary releases the monitor and need to reacquire it when returning from wait(). So, you can have multiple threads that used to own a monitor but now are waiting in wait() method.




回答2:


What means "- locked <0x00007f847612c820> (a com.tc.object.RemoteObjectManagerImpl)".

It means its inside the synchronized block for that lock. It can be with WAITING (in which case another thread can acquire/hold the lock, or RUNNING in which case it is holding the lock.




回答3:


Those threads are waiting to get the lock, not holding the lock.




回答4:


No different threads cannot hold a lock on the same objects. Somebody else holds the lock and all the threads in the dump are waiting on that lock.



来源:https://stackoverflow.com/questions/8926211/can-several-threads-hold-a-lock-on-the-same-monitor-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!