kamranzafar / JCL

Jar Class Loader, a configurable and dynamic custom classloader designed to create, manage and manipulate isolated Java classloaders in IoC frameworks and web applications.
http://kamranzafar.github.com/
579 stars 161 forks source link

Fix ConcurrentModificationException (issue #37) #40

Closed matthoffman closed 8 years ago

matthoffman commented 8 years ago

This addresses issue #37 -- this isn't really a Java 8 specific bug, but the different collection sort behavior makes it more obvious. The list of loaders isn't threadsafe and it's being mutated by lots of threads. You can get all kinds of unexpected behavior here in multithreaded environments (classloaders getting skipped, things loaded from the wrong place, etc).

This change also moves the sort from the load* methods to the add* methods. I don't know why you'd want to resort on every access, that's a very expensive operation. I'd think you could sort on mutation instead.

This does leave the slim chance, I guess, that a user has a custom subclass that is adding to the list directly (loaders.add(...)) and not sorting afterwards; their behavior might change. But there is an "addLoader" method available that behaves appropriately. Ideally, loaders could be made private to remove that possibility, but I didn't go that far.

kamranzafar commented 8 years ago

Thanks for the fix, will pull this update.