Java:Thread在没有获取锁的情况下保持锁定

时间:2013-10-20 11:01:27

标签: java multithreading locking deadlock monitor

我最近遇到了一个非常奇怪的状态的多线程死锁。当它实际上从未在其堆栈跟踪的任何方法中请求锁时,它似乎是一个线程持有一个锁。这是必要的堆栈跟踪:

Found one Java-level deadlock:
=============================
"pool-1-thread-2":
  waiting to lock monitor 0x000000000751fc20 (object 0x00000000f55e2960, a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader),
  which is held by "pool-1-thread-1"
"pool-1-thread-1":
  waiting to lock monitor 0x000000000052ba78 (object 0x00000000f55dfc98, a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager),
  which is held by "pool-1-thread-2"

Java stack information for the threads listed above:
===================================================
"pool-1-thread-2":
    at java.lang.ClassLoader.checkCerts(ClassLoader.java:782)
    - waiting to lock <0x00000000f55e2960> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader)
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:487)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:625)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader.loadClassDirect(CraftElevatorsExtensionManager.java:149)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader.access$4(CraftElevatorsExtensionManager.java:147)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager.findClass(CraftElevatorsExtensionManager.java:85)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    - locked <0x00000000f55dfc98> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:295)
    - locked <0x00000000f55dfc38> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader.loadClass(CraftElevatorsExtensionManager.java:231)
    - locked <0x00000000f55dfc98> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2436)
    at java.lang.Class.privateGetPublicMethods(Class.java:2556)
    at java.lang.Class.getMethods(Class.java:1412)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.elevator.CraftElevatorEventManager.register(CraftElevatorEventManager.java:147)
    at de.ntcomputer.minecraft.elevators2.doorsext.implementation.elevator.CraftElevatorDoorsExtension.onEnable(CraftElevatorDoorsExtension.java:61)
    - locked <0x00000000f55b8e58> (a de.ntcomputer.minecraft.elevators2.doorsext.implementation.elevator.CraftElevatorDoorsExtension)
    at de.ntcomputer.minecraft.elevators2.plugin.api.elevator.extension.CraftElevatorExtension.initialize(CraftElevatorExtension.java:16)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.elevator.CraftElevator.initialize(CraftElevator.java:72)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader.loadSync(CraftElevatorLoader.java:48)
    - locked <0x00000000f55b8f08> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader.access$0(CraftElevatorLoader.java:39)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader$1.run(CraftElevatorLoader.java:64)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.scheduler.CraftElevatorTask.run(CraftElevatorTask.java:53)
    at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftTask.run(CraftTask.java:53)
    at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
"pool-1-thread-1":
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager$ExtensionLoader.loadClass(CraftElevatorsExtensionManager.java:231)
    - waiting to lock <0x00000000f55dfc98> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.extension.CraftElevatorsExtensionManager)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at de.ntcomputer.minecraft.elevators2.extfloors.implementation.elevator.CraftInteractiveElevatorFloorsExtension.deserializeFloor(CraftInteractiveElevatorFloorsExtension.java:116)
    at de.ntcomputer.minecraft.elevators2.extfloors.implementation.elevator.CraftElevatorFloorsExtension$2.deserialize(CraftElevatorFloorsExtension.java:84)
    at de.ntcomputer.minecraft.elevators2.extfloors.implementation.elevator.CraftElevatorFloorsExtension$2.deserialize(CraftElevatorFloorsExtension.java:1)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storagefile.CraftStorageSectionReader.deserializeObject(CraftStorageSectionReader.java:151)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storagefile.CraftStorageSectionReader.deserializeCoreObject(CraftStorageSectionReader.java:199)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storagefile.CraftStorageSectionReader.inflateKeyMapEmpty(CraftStorageSectionReader.java:530)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storagefile.CraftStorageSectionReader.inflateMapValuesEmpty(CraftStorageSectionReader.java:996)
    at de.ntcomputer.minecraft.elevators2.extfloors.implementation.elevator.CraftElevatorFloorsExtension.onEnable(CraftElevatorFloorsExtension.java:76)
    at de.ntcomputer.minecraft.elevators2.extfloors.implementation.elevator.CraftInteractiveElevatorFloorsExtension.onEnable(CraftInteractiveElevatorFloorsExtension.java:70)
    at de.ntcomputer.minecraft.elevators2.plugin.api.elevator.extension.CraftElevatorExtension.initialize(CraftElevatorExtension.java:16)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.elevator.CraftElevator.initialize(CraftElevator.java:72)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader.loadSync(CraftElevatorLoader.java:48)
    - locked <0x00000000f5601ac8> (a de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader.access$0(CraftElevatorLoader.java:39)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.storage.CraftElevatorLoader$1.run(CraftElevatorLoader.java:64)
    at de.ntcomputer.minecraft.elevators2.plugin.implementation.scheduler.CraftElevatorTask.run(CraftElevatorTask.java:53)
    at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftTask.run(CraftTask.java:53)
    at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)

Found 1 deadlock.

如您所见,thread-2尝试锁定&lt; 0x00000000f55e2960&gt;这显然是由thread-1保持的,但是thread-1从未被锁定&lt; 0x00000000f55e2960&gt;在任何方法!这怎么可能?

提前致谢, DevCybran

编辑:添加了使用过的类加载器代码:

public class CraftElevatorsExtensionManager extends ClassLoader implements FilenameFilter, ElevatorsExtensionManager {
    private Map<String,ExtensionLoader> loaders = null;

    ...


    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        for(ExtensionLoader loader: loaders.values()) {
            try {
                return loader.loadClassDirect(name); // line 85
            } catch(ClassNotFoundException e) { }
        }
        throw new ClassNotFoundException();
    }

    ...

    private class ExtensionLoader extends URLClassLoader {

        ...

        private Class<?> loadClassDirect(String name) throws ClassNotFoundException {
            Class<?> loadedClass = this.findLoadedClass(name);
            if(loadedClass==null) return this.findClass(name); // line 149
            return loadedClass;
        }

        ...

        @Override
        protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            // synchronized parent container first  - should prevent deadlocks
            synchronized(CraftElevatorsExtensionManager.this) {
                return super.loadClass(name, resolve); // line 231
            }
        }

    }

}

使用Java 6 u45。

1 个答案:

答案 0 :(得分:0)

我的猜测是你有一个3路死锁。你有机会在应用程序中有其他线程吗?
我看到pool-1-thread-1持有2个锁:0x00000000f55dfc98和0x00000000f5601ac8
pool-1-thread-2保存0x00000000f55e2960,0x00000000f55dfc98等等 难道你有pool-1-thread-3持有0x00000000f5601ac8,0x00000000f55e2960?
或类似的东西。