For some reason, my program suddenly throws a
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)
Exception in thread "Thread-1" java.lang.NegativeArraySizeException
at java.util.AbstractCollection.toArray(Unknown Source)
at java.lang.Thread.run(Unknown Source)
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
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.