Reputation: 1084
I have multiple threads running in a Java application, and all of them need to access the same list. Only one thread, however, actually needs to insert/delete/change the list, and the others just need to access it.
In the other threads, I want to make a copy of the list for me to use whenever I need to read it, but is there any way to do this in a thread safe way? If I had a method to copy the list element by element and the list changed, that would mess it up wouldn't it?
EDIT:
The list will not be deleted from very often, so would it work if I just copied it normally and caught exceptions? If the list grew in the middle of the copy and I missed it, it wouldn't really make a difference to functionality
Upvotes: 2
Views: 2385
Reputation: 1650
Depending on your particular usage, you might benefit from one of these:
Upvotes: 0
Reputation: 1084
I decided to solve this by having a separate thread that handles the thread, with BlockingQueue
for the other threads to submit to if they want to write to the list, or get from the list. If they wanted to write to the list it would submit an object with the content that they wanted to write, and if they wanted to read, it would submit a Future
that the thread with the list would populate
Upvotes: 0
Reputation: 2626
You can use CopyOnWriteArrayList
which is thread safe ,but it create new one on every update process.
Or you can use readWriteLock so when update use no one can read while multiple thread can read simultaneously .
Upvotes: 0
Reputation: 19
Hmm, so I think that what are you looking for is called CopyOnWriteArrayList.
CopyOnWriteArrayList - A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.
Ref: CopyOnWriteArrayList
Upvotes: 1
Reputation: 5361
You can use CopyOnWriteArrayList
for your purpose.
CopyOnWriteArrayList
is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap
in Java.
As name suggest CopyOnWriteArrayList creates copy of underlying ArrayList with every mutation operation e.g. add or set. Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every write operation but its very efficient if you have a List where Iteration outnumber mutation e.g. you mostly need to iterate the ArrayList and don't modify it too often.
With this collection, you shouldn't create a new instance every time. You should have only one object of this and it will work.
Upvotes: 3
Reputation: 593
Use Collections.synchronizedList().
Example :
Collections.synchronizedList(new ArrayList<YourClassNameHere>())
Upvotes: -1