需要审阅:ConcurrentmodificationException,而ReentrantLock确保这是不可能的

时间:2019-03-15 16:32:18

标签: java thread-safety concurrentmodification

上下文:Github: issue 20Github: issue 22

我有一个ConcurrentModificationException,但是我不知道在哪里看。变量是私有的,并且只能在使用ReentrantLock锁定了所有访问权限的同一类中访问。

在循环中,不会删除对象(所以为什么会发生此异常?)

这是细分的代码:

private static final TreeMap<Long, TrackerHolder> TRACKER_LIST = new TreeMap<>();
private static final ReentrantLock TRACKER_LIST_LOCK = new ReentrantLock();

public static void tickUntil(long time){
    TRACKER_LIST_LOCK.lock();
    boolean hasWork = true;
    while(System.nanoTime() < time && hasWork) {
        hasWork = false;
        for(TrackerHolder holder : TRACKER_LIST.values()){ //This line is giving the error, indicating TRACKER_LIST is being modified somehow.
            Tracker tracker = holder.getTracker();
            if(tracker.needsTick()){
                hasWork = true;
                tracker.grantTick();
            }
        }
    }
    TRACKER_LIST_LOCK.unlock();
}

这是一个开源项目,您可以访问here(链接到当前时间点。)

java.util.ConcurrentModificationException: null
    at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1211) ~[?:1.8.0_191]
    at java.util.TreeMap$ValueIterator.next(TreeMap.java:1256) ~[?:1.8.0_191]
    at cf.terminator.tiquality.tracking.TrackerManager.tickUntil(TrackerManager.java:69) ~[TrackerManager.class:?]
    at cf.terminator.tiquality.monitor.TickMaster.onServerTick(TickMaster.java:64) ~[TickMaster.class:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_2625_TickMaster_onServerTick_ServerTickEvent.invoke(.dynamic) ~[?:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
    at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:746) ~[EventBus.class:?]
    at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:696) ~[EventBus.class:?]
    at net.minecraftforge.fml.common.FMLCommonHandler.onPostServerTick(FMLCommonHandler.java:266) ~[FMLCommonHandler.class:?]
    at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:712) ~[MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:526) [MinecraftServer.class:?]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]

我正需要其他人对此进行检查。我还想了解这个ConcurrentModificationException的来源以及将来如何防止它。我认为自己有能力编写相当高级的代码,但这里可能会遗漏一些东西。

谢谢您的时间。

0 个答案:

没有答案