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)

[1336 byte] By [Christian-Luca] at [2007-10-3 6:28:34]
# 1

Hi,

I did like what you did, its working fine,

HashMap hm = new HashMap();

hm.put("1","a");

hm.put("2","b");

hm.put("3","c");

TreeMap tm= new TreeMap(hm);

System.out.println("Map: " + tm.get("1"));

rgds

Dhinakar

dhinakaran_apeca at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...
# 2

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

christian-luca at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...
# 3

Well, your execution stops within the Map's putAll method*, but, are you sure it's the Map causing the endless loop or maybe your code loops endlessly that calls the putAll again and again. I'd advice to check the calling code rather than the Map's code.

*If you are using Java 1.5.0_8, line 314 is outside the loop in putAll().

stefan.schulza at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...
# 4

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}

christian-luca at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...
# 5

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

stefan.schulza at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...
# 6

If you are getting an infinate loop in the TreeMap constructor it is likely that your comapreTo method (or Comparable instance) is incorrect and is looking at the contents of other items in the Map.

post the compareTo (or Comaprable) method so we can look at it.

cheers

matfud

matfuda at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...
# 7

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

jverda at 2007-7-15 1:15:15 > top of Java-index,Core,Core APIs...