I have the following situation:
- A data structure that can only be extended (I only ever add things to the tail)
- I need to be able to track which items I already have (I have an index, and ideally I want to start moving the list again from that particular item)
- I would like reads never to be blocked, and adding a new element only blocks the tail of the queue, not the entire queue
This is a structure that is heavily modified by multiple threads.
What would be the best data structure for this?
ArrayList . This would be ideal to be able to directly access the last item, apparently using an index, but this leads to the exclusion of concomitant modifications. I could make it synchronized, but would like to avoid blocking (or any blocking separately from the very last element, as this is the only one where there may be simultaneous entries to add new elements)
ConcurrentLinkedQueue . This would solve my concurrency problem, but the problem is that I would need to keep the current iteration position, not the integer index. This has a problem that it returns a weakly matched iterator, which does not guarantee the return of new objects that have been added to the list since the iterator was created (source: javadoc)
ConcurrentHashMap with an index in the form of keys. This has the advantage that I can directly access the data corresponding to the correct index, but there is a problem that there is no getNext statement that will allow me to efficiently move elements from the index, index + 1, etc.
Vectors . This would solve most of my problems by resolving something that would not throw simultaneous modification exceptions and allow direct access. However, given that all methods are synchronized, performance is poor compared to arraialists. Given that I only want to expand the structure, and not insert records in the middle, I do not want to go for this heavy weight solution, where reading also suffers from a performance hit (whereas, given my usecase, the element index never changes, so there is no need to synchronize readings that are not tail)
Custom data structure : save the array of objects that I want to save, and the pointer to the tail of this array (the last set of elements), when inserting a new object, lock the tail and the object that the tail points to. When an object exceeds its current size, the lock operation changes.
What would be a better strategy / any other more efficient implementation?
java concurrency data-structures
user1018513 Apr 25 '13 at 9:28 2013-04-25 09:28
source share