Originally posted by Lee Barney:
How about this. Something I just thought of.
Create a fair semaphore that has Integer.MAX_VALUE permits.
When each iteration begins, aquire a single permit and release it when iteration is complete.
When each removal begins, aquire Integer.MAX_VALUE permits and release them after the removal is complete.
The removal will happen in fifo order since the semaphore is initialized as fair and will not begin until all iteration requests prior to the request for the removal are complete. In addition to this, all new iteration or removal requests will wait until the removal is complete.
I know that a fair semaphore is not as fast as a non-fair one.
Any other ideas?
What you are basically describing is a reader-writer lock. You are just simulating the functionality with a semaphore. A couple of notes:
1. Grabbing MAX_VALUE permits is fine provided that you release the original one. Or you will get a case, where 2 threads each hold one permit, but is waiting for MAX_VALUE permits.
2. And I not sure, but I don't think the iterator will work in parallel. It may still throw a Concurrent Exception, if it encounters something that it did not directly change.
Henry