Java线程转储:BLOCKED线程没有“等待锁定…”

24

我在理解从jstack获取的用于Tomcat 6(java 1.6.0_22,Linux)上运行的Spring MVC Web应用程序的线程转储方面遇到了困难。

我看到一些阻塞线程(导致其他线程等待),它们本身也被阻塞,但是线程转储没有告诉我它们为什么或者它们正在等待哪个监视器。

例如:

"TP-Processor75" daemon prio=10 tid=0x00007f3e88448800 nid=0x56f5 waiting for monitor entry [0x00000000472bc000]
    java.lang.Thread.State: BLOCKED (on object monitor)
        at java.lang.Class.initAnnotationsIfNecessary(Class.java:3067)
        - locked <0x00007f3e9a0b3830> (a java.lang.Class for org.catapultframework.resource.ResourceObject)
        at java.lang.Class.getAnnotation(Class.java:3029)
        ...

我的意思是,堆栈跟踪中缺少“等待锁定…”行。 显然,线程锁定了一个Class对象,但我不明白为什么线程本身被阻塞。

线程转储没有包含任何死锁的提示。

我该如何识别锁定监视器?

谢谢, Oliver


你是否看到了参考0x00000000472bc000的其他条目? - John Vint
不,不在同一个转储文件中。0x00000000472bc000 标识线程“TP-Processor75”,所以您期望在转储文件中多次提到相同的线程吗? - Oliver
0x00000000472bc000 表示线程正在等待进入监视器。也就是说,另一个线程已经进入了监视器引用 0x00000000472bc000,而 TP-Processor75 正在等待当前持有线程释放 0x00000000472bc000。 - John Vint
4个回答

12

8

检查终结器线程是否被阻塞或等待。

在进行GC清理时,GC将“停止世界”以执行其清理操作。 “世界”的定义取决于正在使用和上下文的垃圾收集器。它可以是一小群线程或全部线程。在正式收集垃圾之前,GC将调用对象的finalize()方法。

如果您处于不希望的情况下,需要实现终结器方法,则可能会出现终结代码阻止完成且“世界”保持停止状态的情况。

当看到许多线程被某种未知的神奇力量永久阻塞时,这种情况最为明显:查找导致阻塞的代码,但是它没有任何意义;附近也找不到任何阻塞代码,并且转储不会透露它正在等待的监视器,因为根本不存在一个。此时,GC已经暂停了线程。


3
我刚刚在谷歌浏览器中使用Applet时遇到了类似的问题。
简言之:
1. 当虚拟机需要加载一个类时,会导致线程阻塞。 2. 如果加载类的过程本身被某个东西阻塞,整个应用程序可能会冻结。
详细解释:
我遇到了以下情况:
1. 我在Chrome中使用一个Applet,代码库为单个class文件的文件夹(没有jar)。 2. 网站使用LiveConnect将焦点事件传递给Applet。 3. JS调用使用带有new Runnable() ...的Executor来分离调用,以减少等待时间和JS中的挂起。 4. 问题就出在这里!
解释:
1. new Runnable()是一个匿名内部类,在JS调用发生之前尚未加载。 2. 因此,JS调用触发类加载。 3. 但现在类加载器被阻塞了,因为它需要通过与处理传入JS调用的相同队列或机制对浏览器进行通信(我猜测)。
这是正在尝试加载类的阻塞线程:
"Thread-20" daemon prio=4 tid=0x052e8400 nid=0x4608 in Object.wait() [0x0975d000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    at sun.plugin2.message.Queue.waitForMessage(Unknown Source)
    - locked <0x29fbc5d8> (a sun.plugin2.message.Queue)
    at sun.plugin2.message.Pipe$2.run(Unknown Source)
    at com.sun.deploy.util.Waiter$1.wait(Unknown Source)
    at com.sun.deploy.util.Waiter.runAndWait(Unknown Source)
    at sun.plugin2.message.Pipe.receive(Unknown Source)
    at sun.plugin2.main.client.MessagePassingExecutionContext.doCookieOp(Unknown Source)
    at sun.plugin2.main.client.MessagePassingExecutionContext.getCookie(Unknown Source)
    at sun.plugin2.main.client.PluginCookieSelector.getCookieFromBrowser(Unknown Source)
    at com.sun.deploy.net.cookie.DeployCookieSelector.getCookieInfo(Unknown Source)
    at com.sun.deploy.net.cookie.DeployCookieSelector.get(Unknown Source)
    - locked <0x298da868> (a sun.plugin2.main.client.PluginCookieSelector)
    at sun.net.www.protocol.http.HttpURLConnection.setCookieHeader(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    - locked <0x2457cdc0> (a sun.net.www.protocol.http.HttpURLConnection)
    at com.sun.deploy.net.HttpUtils.followRedirects(Unknown Source)
    at com.sun.deploy.net.BasicHttpRequest.doRequest(Unknown Source)
    at com.sun.deploy.net.BasicHttpRequest.doGetRequestEX(Unknown Source)
    at com.sun.deploy.cache.ResourceProviderImpl.checkUpdateAvailable(Unknown Source)
    at com.sun.deploy.cache.ResourceProviderImpl.isUpdateAvailable(Unknown Source)
    at com.sun.deploy.cache.DeployCacheHandler.get(Unknown Source)
    - locked <0x245727a0> (a java.lang.Object)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    - locked <0x24572020> (a sun.net.www.protocol.http.HttpURLConnection)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    - locked <0x299726b8> (a sun.plugin2.applet.Applet2ClassLoader)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    - locked <0x299726b8> (a sun.plugin2.applet.Applet2ClassLoader)
    at java.lang.ClassLoader.loadClass(Unknown Source)

你可以看到,它正在等待一条消息 --> waitForMessage()
同时,在这里我们的JS调用被阻止了。
"Applet 1 LiveConnect Worker Thread" prio=4 tid=0x05231800 nid=0x1278 waiting for monitor entry [0x0770e000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at MyClass.myMethod(MyClass.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin.javascript.Trampoline.invoke(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass$MethodInfo.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass$MemberBundle.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass.invoke0(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass.invoke(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$DefaultInvocationDelegate.invoke(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo.doObjectOp(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$LiveConnectWorker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

其他线程也以同样的方式被阻塞。我猜测所有后续的类加载请求都被第一个被阻塞的类加载线程所阻塞。

如前所述,我的猜测是类加载过程被未处理的JS调用所阻塞,而这本身又被待加载的类阻塞。

解决方案:

  1. 在applet的构造函数中触发加载所有相关的类,在从JS进行任何调用之前加载。
  2. 如果不是单独加载类文件,而是从jar文件中加载,可能会有所帮助。这背后的理论是:类加载器不需要与浏览器通信来从jar文件中加载类(这将是
  3. 与1.结合使用:使用动态代理类来包装所有传入的JS调用,并在Executor中独立运行它们。

我的#3实现:

public class MyClass implements JsCallInterface
{

    private final JsCallInterface jsProxy;

    private final static interface JsCallInterface
    {
        public void myMethod1Intern(String param1, String param2);
    }

    private final class JsCallRunnable implements Runnable
    {

        private final Method method;
        private final Object[] args;

        private JsCallRunnable(Method method, Object[] args)
        {
            this.method = method;
            this.args = args;
        }

        public void run()
        {
            try
            {
                method.invoke(MyClass.this, args);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public MyClass()
    {
        MyUtilsClass.class.getName(); // load class
        JsCallRunnable.class.getName(); // load class
        InvocationHandler jsCallHandler = new InvocationHandler()
        {

            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
            {
                MyUtilsClass.executeInExecutor(new JsCallRunnable(method, args));
                return null;
            }

        };
        jsProxy = (JsCallInterface) Proxy.newProxyInstance(MyClass.class.getClassLoader(), new Class<?>[] { JsCallInterface.class }, jsCallHandler);
    }

    public void myMethod1(String param1, String param2)
    {
        jsProxy.myMethod1Intern(param1, param2);
        // needs to be named differently than the external method or else the proxy will call this method recursively
        // alternatively the target-class in "method.invoke(MyClass.this, args);" could be a different instance of JsCallInterface
    }

    public void myMethod1Intern(String param1, String param2)
    {
        // do actual work here
    }
}

3
这是 Oracle 的 HotSpot JVM 中的一个小问题 - 在您的堆栈跟踪中,您看到 - locked <0x00007f3e9a0b3830> 实际上应该是 - waiting to lock <0x00007f3e9a0b3830>
更多详情请参见此错误

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接