gpt4 book ai didi

java - HashMap 上的 NegativeArraySizeException

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:29:38 26 4
gpt4 key购买 nike

由于某些原因,我的程序在运行一段时间后突然抛出一个NegativeArraySizeException。抛出它的代码在一个命令后面,我在抛出异常之前输入了该命令。

我使用的代码主要用于调试目的,如下所示:

final HashMap<String, Integer> busy = new HashMap<>();
//this map gets filled and emptied in threads

System.out.println("Busy tables: " + Arrays.toString(this.busy.keySet().toArray()));
System.out.println("Time busy: " + Arrays.toString(this.busy.values().toArray()));
//map gets read (from the input handler thread)

异常在第一行 System.out.println() 抛出,但我可以想象如果它继续运行它也会在另一行抛出。

这可能是某种线程问题还是其他原因?

谷歌(多年来第一次)没有给我可用的结果。无论如何,Set 怎么可能有负数?

编辑:异常:

Exception in thread "Thread-1" java.lang.NegativeArraySizeException
at java.util.AbstractCollection.toArray(Unknown Source)
at nl.lolmewn.statsglobal.Main.handleInput(Main.java:61)
at nl.lolmewn.statsglobal.Main.access$000(Main.java:20)
at nl.lolmewn.statsglobal.Main$1.run(Main.java:200)
at java.lang.Thread.run(Unknown Source)

最佳答案

如果没有关于您的代码的更多信息,我认为对 HashMap 对象的未 protected 多线程访问是主要嫌疑人。 HashMap ,与Hashtable相反,是同步的,需要显式锁才能在多线程环境中工作,直接或通过 keySet()entrySet()< 返回的集合

如果不这样做,由于 HashMap 处于不一致的内部状态,可能会产生很多有趣的副作用。我建议使用调试器来中断该异常并更好地了解发生了什么。

关于java - HashMap 上的 NegativeArraySizeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14104897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com