wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Weak references and ConcurrentModificationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Weak references and ConcurrentModificationException" Watch "Weak references and ConcurrentModificationException" New topic
Author

Weak references and ConcurrentModificationException

Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
I need to maintain a weak list of dialogs, like this:



Every once in a while, I clear the old weak references from this list:



Problem is, I get an exception



I do understand what's happened... sort of... OK, maybe I don't... the garbage collection thread (or whatever) is clearing my weak reference as we are accessing it... However, since no other thread is modifying my list (only mutes its elements), how is this condition even detected? And what's the right way to maintain a weak collection of objects then?

Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
Turns out that weak references and multithreading have nothing to do with it; you simply can not delete an element of a collection while iterating through the same collection. Mod, feel free to delete.. or keep for a reference in case someone else encounters it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Jane Dodo wrote:Turns out that weak references and multithreading have nothing to do with it; you simply can not delete an element of a collection while iterating through the same collection. Mod, feel free to delete.. or keep for a reference in case someone else encounters it.

Actually you can, but you need to use the iterator to perform the deletion:

This also assumes that no other code is modifying the list while you do this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weak references and ConcurrentModificationException