HashMap上的NegativeArraySizeException [英] NegativeArraySizeException on a HashMap

查看:158
本文介绍了HashMap上的NegativeArraySizeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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给了我(这是第一次)没有可用的结果。

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

编辑:例外:

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 对象进行无保护的多线程访问是主要的嫌疑犯。 HashMap ,与 Hashtable 不是 同步的,并且需要显式锁定以在多线程环境中工作,可以直接或通过 keySet() entrySet()

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().

由于 HashMap 有趣的副作用处于不一致的内部状态。我建议使用调试器打破这个异常,并更好地了解发生了什么。

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.

这篇关于HashMap上的NegativeArraySizeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆