NegativeArraySizeException on a HashMap

依然范特西╮ 提交于 2019-12-12 15:04:28

问题


For some reason, my program suddenly throws a NegativeArraySizeException after running for a while. The code that throws it is behind a command, which I've entered before the exception is thrown.

The code I'm using is mostly for debug purposes, and is the following:

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)

The exception is thrown on the first System.out.println() line, but I can imagine it being thrown on the other one too if it'd continued running.

Could it be some sort of Threading issue or could the cause be somewhere else?

Google gave me (for the first time in ages) no usable results. How can a Set have a negative size anyway?

Edit: The exception:

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)

回答1:


Without more information about your code, I would consider unprotected multi-threaded accesses to the HashMap object to be the prime suspect. HashMap, contrary to Hashtable, is not synchronized and needs explicit locks to work in a multi-threaded environment, either directly or through the sets returned via keySet() or entrySet().

Failing to do that can create a lot of interesting side-effects due to the HashMap being in a inconsistent internal state. I would suggest using a debugger to break on that exception and have a better look at what is going on.



来源:https://stackoverflow.com/questions/14104897/negativearraysizeexception-on-a-hashmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!