加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – HashMap上的NegativeArraySizeException

发布时间:2020-12-15 08:49:12 所属栏目:Java 来源:网络整理
导读:出于某种原因,我的程序在运行一段时间后突然抛出NegativeArraySizeException.抛出它的代码是在命令之后,我在抛出异常之前输入了该命令. 我正在使用的代码主要用于调试目的,如下所示: final HashMapString,Integer busy = new HashMap();//this map gets fil
出于某种原因,我的程序在运行一段时间后突然抛出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()行上抛出异常,但我可以想象如果它继续运行它也会抛出另一个.

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

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

编辑:例外:

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对象的无保护多线程访问是主要的嫌疑.与 Hashtable相反,HashMap不同步,需要显式锁定才能在多线程环境中直接或通过keySet()或entrySet()返回的集合.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读