While working through the Jess source code in an attempt to figure out the problem with java.util.ConcurrentModificationException I was having with undefinstance, I came across what may be an implementation bug for handling (undefinstance *).
ReflectFunctions.UnDefinstance.call() uses DefinstanceList.listDefinstances() which returns m_definstances.keySet().iterator(). However, this iterator does not appear to be synchronized around the class instance or the map. The actual removal is deferred to DefinstanceList.undefinstance(). DefinstanceList.undefinstance() invokes the remove() method on the HashMap directly (line 216). This method is synchronized around the DefinstanceList instance and the Rete compiler. Since the removal is invoked directly on the HashMap and not on the iterator, this can lead to java.util.ConcurrentModificationExceptions being thrown... >From java.util.HashMap API documentation: The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. I have fixed this in the interim by modifying DefinstanceList.listDefinstances() to return an Iterator to a copy of the actual keyset instead of the real collections keyset. This way when undefinstance() is called it can freely modify the underlying Collection of definstances without causing trouble to the Iterator. Am I way off base here, or does this make sense? Jason --------------------------------------------------------------------- To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]' in the BODY of a message to [EMAIL PROTECTED], NOT to the list (use your own address!) List problems? Notify [EMAIL PROTECTED] ---------------------------------------------------------------------
