java - Concurrent ArrayList -


I need an array like an ArrayList which only functions

  • < Code> get index (int index)
  • add (e element)
  • set (int index, element E) <
  • iterator ()

Due to the use of the iterator in many places, collection # indexed synchronized By using also error prone this list can increase to some thousand elements and flowing Of which some use, so I'm pretty sure that CopyOnWriteArrayList will be very slow, I'll start to avoid premature optimization, but I'm sure it will not work well.

Most of the access will read single-threaded. That's why I am asking what is the proper data structure for it.


Although it provides a synchronized errator, it invokes the synchronized list in something, but it will not be the concise improvements concept Due to encouraging concurrent behavior, I am obviously required that all the changes will be seen by later reading and Dwarkas.

The itater does not need to show a consistent snapshot, it can be updated or used by the set (int index, e element) as the only one operation Replace the item with its updated version (with some additional information that is irrelevant to the user of the itater). Items are completely immutable.


I have explicitly said why CopyOnWriteArrayList will not do this. ConcurrentLinkedQueue is out of the question because there is no indexed access in it. I need only some operations instead of the fully developed arrelist , so long as any Java concurrent The list-related question is not a duplicate, it's not the one.

In your case, you can use it to access a supported list; You can get permission to read your list. There is a need to write a thread only then all the reader-threads will have to wait to complete the operation. Javadak makes it clear:

A ReadWriteLock keeps a pair of related locks, one for reading and for just writing. Reading lock can be organized simultaneously by multiple reader threads, unless there is no author. The written lock is unique.

Here's a sample:

  Public class concurrent analytics {lt; T & gt; {/ ** Use it to lock to write such as add / remove * / private finals lock lock lock; / ** Use it to lock for reading tasks such as Mill / Iterator / Lock. * / Private Final lock lock lock; / ** Inherent List * / Personal Final List & lt; T & gt; List = new ArrayList (); {ReentrantReadWriteLock rwLock = New ReentrantReadWriteLock (); ReadLock = rwLock.readLock (); WriteLock = rwLock.writeLock (); } Add Public Zero (TE) {writeLock.lock (); Try {list.add (e); } Finally {writeLock.unlock (); }} Public {int Index} {readLock.lock (); Try {List.get (index); } Finally {readLock.unlock (); }} Public iterator & lt; T & gt; Iterator () {readLock.lock (); Try {new array list & lt; T & gt; (List) .iterator (); // ^ We repeat on a snapshot of our list} Finally {readLock.unlock (); }}  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -