HashMap上的NegativeArraySizeException

Lol*_*ewn 3 java hashmap

出于某种原因,我的程序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)
Run Code Online (Sandbox Code Playgroud)

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

它可能是某种线程问题还是原因可能在其他地方?

谷歌给了我(这是多年来的第一次)没有可用的结果.套装怎么能有负面尺寸?

编辑:例外:

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)
Run Code Online (Sandbox Code Playgroud)

thk*_*ala 7

如果没有关于代码的更多信息,我会认为对HashMap对象的无保护多线程访问是主要的嫌疑.HashMap,违背Hashtable,是同步的,需要明确的锁在多线程环境中工作,直接或经由返回的集合keySet()entrySet().

由于处于不一致的内部状态,未能做到这一点会产生许多有趣的副作用HashMap.我建议使用调试器来打破该异常,并更好地了解正在发生的事情.