loop within java.util.AbstractMap.putAll(..)
Hi all,
trying to transform an HashMap to a TreeMap
(i.e. TreeMap tm = new TreeMap(myHashMap); )
I randomly get an infinite loop within java.util.AbstractMap.putAll(..)
(see dump below from a kill -quit on the looping program)
Why would an hashmap cause such a problem ?
(I must admit that before being used within the new TreeMap instruction,
this hashmap has been populated through several threads - that are dead at time it goes through new TreeMap()
Thanks for any suggestion
Christian-Luc
"main" prio=5 tid=0x000560d8 nid=0x1 runnable [ffbee000..ffbeefe8]
at java.util.AbstractMap.putAll(AbstractMap.java:314)
at java.util.TreeMap.putAll(TreeMap.java:327)
at java.util.TreeMap.<init>(TreeMap.java:155)
at outilsBatch.suiviExecution.GenBatchStdReporting.detailChargementIMs(GenBatchStdReporting.java:920)
at outilsBatch.suiviExecution.GenBatchStdReporting.produireSynthReport(GenBatchStdReporting.java:623)
at outilsBatch.suiviExecution.GenBatchStats.produireReports(GenBatchStats.java:518)
at outilsBatch.GenBatchLanceur.finirStatistiquesEtSuiviExecution(GenBatchLanceur.java:897)
at outilsBatch.GenBatchLanceur.run(GenBatchLanceur.java:741)
at outilsBatch.GenBatchLanceur.main(GenBatchLanceur.java:533)
Thanks for the answer.
I agree it works ... it would be very inconvenient if Sun would deliver something that *really* did not work :-)
The problem that I raise here is that it happens randomly, in the case where the hashmap used to construct the treemap has been updated through several threads.
I should rephrase my question: can this loop happen because the hashmap structure has been damaged, and if this is the case would it be cleaner if the TreeMap constructor checked the hashmap structure ?
Thanks,
Christian-Luc
I am using java 1.4.2_05
and sure: new TreeMap(myHashMap) is called just once.
So, when I saw the code below, I suspected that if something is wrong in the map, then it may shake the iterator and "while (i.hasNext())" may always get true ?
312 public void putAll(Map t) {
313Iterator i = t.entrySet().iterator();
314while (i.hasNext()) {
315Entry e = (Entry) i.next();
316put(e.getKey(), e.getValue());
317}
318}
> So, when I saw the code below, I suspected that if
> something is wrong in the map, then it may shake the
> iterator and "while (i.hasNext())" may always get
> true ?
Wonder, what that should be. The Iterator implementation of HashMap is fail-fast, i.e, if the map is modified while iterating, an exception is thrown. What is the context in that you call the putAll(). If there is any loop, check this one first.
> The problem that I raise here is that it happens
> randomly, in the case where the hashmap used to
> construct the treemap has been updated through
> several threads.
If you properly synchronized, that won't be a problem. If you didn't, it might.
Or it might be that the map contains itslef or portions of itself.
Or it might be that your compare/compareTo is bogus.