• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

threads, iteration and removal

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to be able to allow iteration over a collection by multiple concurrent threads, but if a removal method is called then all new iterations must wait until the removal happens. Also, the removal must wait until all of the threads iterating over the collection have completed.

Ideas?
 
Lee Barney
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simplest solution that I can think of is to delegate all operations on your collection to a class. Now, instead of sharing the collection itself among all the threads, you would share an instance of that class:

 
reply
    Bookmark Topic Watch Topic
  • New Topic