几个线程可以在Java的同一监视器上保持锁吗? [英] Can several threads hold a lock on the same monitor in Java?

查看:140
本文介绍了几个线程可以在Java的同一监视器上保持锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们正在分析tomcat线程转储.在同一时间运行的所有线程的单线程转储 Tomcat上的时间包含以下几行:

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)

尤其是我们不了解

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

根据我们的理解,它说三个线程当时正在锁定同一监视器.在我们的理解中 并且根据 JLS 这是不可能的.

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?

推荐答案

看起来所有这些线程都在等待与监视器关联的条件,即它们调用了该监视器的wait()方法.

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

当线程在其拥有的监视器上调用wait()时,它会暂时释放监视器,并在从wait()返回时需要重新获取它.因此,您可以拥有多个线程,这些线程曾经拥有一个监视器,但是现在正在wait()方法中等待.

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.

这篇关于几个线程可以在Java的同一监视器上保持锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆