Is there a concurrent List in Java's JDK?
There is a concurrent list implementation in java.util.concurrent. CopyOnWriteArrayList in particular.
Choosing the best concurrency list in Java
had better be
List
The only List
implementation in java.util.concurrent
is CopyOnWriteArrayList. There's also the option of a synchronized list as Travis Webb mentions.
That said, are you sure you need it to be a List
? There are a lot more options for concurrent Queue
s and Map
s (and you can make Set
s from Map
s), and those structures tend to make the most sense for many of the types of things you want to do with a shared data structure.
For queues, you have a huge number of options and which is most appropriate depends on how you need to use it:
- ConcurrentLinkedQueue
- ArrayBlockingQueue
- LinkedBlockingDeque
- LinkedBlockingQueue
- PriorityBlockingQueue
- SynchronousQueue
- DelayQueue
Concurrent List data structure for reading file and storing objects
Not really. Your ArrayList strategy is as good as it gets and is e.g. equivalent to what parallelStream().collect(toList())
does.
java concurrent Array List access
The documentation answers your question.
It is imperative that the user manually synchronize on the returned list when iterating over it:
List list = Collections.synchronizedList(new ArrayList());
...
synchronized(list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
As for contains
and remove
, you shouldn't have to synchronize manually. I'm looking at the source code of Collections
and it looks like it does that for you:
public boolean contains(Object o) {
synchronized (mutex) {return c.contains(o);}
}
public boolean remove(Object o) {
synchronized (mutex) {return c.remove(o);}
}
It wouldn't be a synchronized list if you have to do this stuff on your own.
A concurrent collection that maintains insertion order
If you have mostly read operations, very few write operations and you don't have too much elements then you can use CopyOnWriteArrayList
as it is a lock free implementation of a List
for read operations such that it can hardly be faster but it is very costly for the write operations as for every single write, it re-builds the entire List
to be able to provide a new read-only copy for the next read operations.
As in your case, you have many write operations and a lot of elements to put in your collection, CopyOnWriteArrayList
is clearly not an option for you.
What I suggest in your case, is to use one thread-safe Queue
that you can find into the java.util.concurrent
package. According to your context and your JDK version the best choice may change, but if you don't specifically need a blocking queue
or a deque
but only a pure collection
, the best choices are probably ArrayBlockingQueue
, ConcurrentLinkedQueue
or LinkedBlockingQueue
but according to this benchmark result (a little bit old), the LinkedBlockingQueue
provides the best overall performances.
But when we talk about performances, the first and most important advice is: always test on your target environment, it is the only valid way to know what is the best choice for you.
Concurrent access of List of Map in Java
Assuming the following:
- The
inputList
list is populated by one thread, and then it is not modified (i.e. no elements are added/removed) by other threads - No two threads access the same map object in the
input
map (as the question states)
If this is the case, then there is no need to synchronize on inputList
nor on input
.
If, on the other hand, you will access inputList
itself by other threads, use Collections.syncrhonizedList() for example.
Related Topics
How to Rotate an Image Gradually in Swing
How to Directly Initialize a Hashmap (In a Literal Way)
Under What Conditions Is a Jsessionid Created
Differencebetween Up-Casting and Down-Casting with Respect to Class Variable
Implementing Two Interfaces in a Class with Same Method. Which Interface Method Is Overridden
Java.Sql.Sqlexception: - Ora-01000: Maximum Open Cursors Exceeded
Why Does Ssl Handshake Give 'Could Not Generate Dh Keypair' Exception
Removing an Element from an Array (Java)
How to Upload File Using Selenium Webdriver in Java
Why Does Runtime.Exec(String) Work for Some But Not All Commands
Difference Between Regex [A-Z] and [A-Za-Z]
Replace Implicit Wait with Explicit Wait (Selenium Webdriver & Java)
Places Where Javabeans Are Used
How to Convert Java String into Byte[]
Why Is Hibernate Open Session in View Considered a Bad Practice