Synchro between Enumeration and Hashtable

Hello

Is this dangerous to work at the same time with a Hashtable and an Enumeration on it ?

For example, I'm using a Hashtable to store some objets... these objets are very often put/remove/get.

Periodicaly, a "cleaner" is waking up and using an enumeration on this table to clean (remove) expired objects from Hashtable...

Thanks

Ludovic

[378 byte] By [ludovicmaillet] at [2007-9-30 21:20:33]
# 1

> Is this dangerous to work at the same time with a

> Hashtable and an Enumeration on it ?

I'd guess "yes".

Hashtable's javadoc (check it out) warns against Iterators, but not so clearly against Enumerations. It does mention that Enumerations aren't checked as eagerly against concurrent modification.

A quick hack is to do the enumeration inside synchronize(myHashtable). It should work, though it feels like a bit of a dirty trick to me - like mucking with Hashtable's internal implementation. Preferably lock on some other suitable object - probably the one that contains the Hashtable.

sjasja at 2007-7-7 2:52:50 > top of Java-index,Administration Tools,Sun Connection...