This question already has answers here:
In what situations is the CopyOnWriteArrayList suitable? [duplicate]
(2 answers)
Closed 5 years ago.
I can't find any explanation on SO about when it's applicable to use CopyOnWriteArrayList, I mean in what situation the container suits best.
There is a question with same title, In what situations is the CopyOnWriteArrayList suitable?. However, it's closed because it duplicates against other question thread. I think both threads haven't answered the question so I launched this question thread.
For In what situations is the CopyOnWriteArrayList suitable?, it actually illuminates that the CopyOnWriteArrayList is
very efficient if you have a List where Iteration outnumber mutation
But I didn't find the actual application scenario. For How can CopyOnWriteArrayList be thread-safe?, it actually focuses on the reason why the CompyOnWriteArrayList is thread safe.
Thanks for your answer in advance.
Based on the javadoc,
A thread-safe variant of java.util.ArrayList in which all mutative
operations (add, set, and so on) are implemented by making a fresh
copy of the underlying array.
As you can see, any modification to the container is costly especially when the size of data in the container is big.
It's better to use CopyOnWriteArrayList when the frequency of modification is low. As suggested by JCIP,
the copy-on-write collections are reasonable to use only when
iteration is far more common than modification.
For example the event-notification system. It will iterate the container every time an event needs to be notified but will only be modified when the observer is changed. The occurrence of the former is with high frequency and the latter is with low frequency.
Related
This question already has answers here:
Java ArrayList.add() method thread safe for purely parallel adding? [duplicate]
(6 answers)
Closed 3 years ago.
I want to add information to a List and that information is gathered from multiple threads. The threads do not need to pull data from the list. Is it safe to have a regular ArrayList or LinkedList? Or what other ways can I do this?
Regular collections are not thread safe, but there are ways to circunvent that:
You can use CopyOnWriteArrayLists, but they are very expensive to write, you can also create a Syncronized Collection, which are not so expensive to write, and you can also work with Queues, it will depend on what you want to achieve.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I just don't get it. What are streams, how to use them and how to use Lambda expressions with it?
From the Javadoc:
A sequence of elements supporting sequential and parallel aggregate
operations
I think the below is key, however. The stream is making the elements of the collection available for the downstream operations.
A stream pipeline, like the "widgets" example above, can be viewed as
a query on the stream source. Unless the source was explicitly
designed for concurrent modification (such as a ConcurrentHashMap),
unpredictable or erroneous behavior may result from modifying the
stream source while it is being queried.
Note also:
Collections and streams, while bearing some superficial similarities,
have different goals. Collections are primarily concerned with the
efficient management of, and access to, their elements. By contrast,
streams do not provide a means to directly access or manipulate their
elements, and are instead concerned with declaratively describing
their source and the computational operations which will be performed
in aggregate on that source
If you're at all familiar with Scala (and noting its apparent absence of streams), it would be worth looking at this article too, which details the collection/stream differences, with particular focus on Java vs. Scala.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've heard that you shouldn't use foreach loop where you can avoid it because it can cause thread concurrency, what does it mean thread concurrency, and is it true ?
So when should you use the for-each loop?
Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel.
These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.
Emphasis mine.
Unless you clear up what you mean with exactly, this would indicate that you're wrong.
Using a for each loop when altering a collection can trigger it to incorrectly detect a concurrent modification which looks like it has been modified by another thread when in fact it has been modified by the same thread.
"for each" doesn't have anything to do with concurrency. You can find the ConcurrentModicationException a number of way, one is to use Iterator.remove().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to add software enforced copy-on-write for multithreaded applications in Java? By this I mean threads having a reference to the same object, but when one thread attempts to modify it, the object pointed to is copied and the reference is adjusted to point to that copy.
The only implementation I know is the
java.util.concurrent.CopyOnWriteArrayList
see
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
and the related Set class
java.util.concurrent.CopyOnWriteArraySet
and finally
org.apache.mina.util.CopyOnWriteMap
but it depends from your need.
If your question is,
is it possible to enforce copy-on-write behavior across the board for an entire Java runtime
then the answer is,
No, there is no such general capability in Java.
Actually, I think the closest you can possibly get to that goal is using Clojure. All its default data structures are copy-on-write internally, and on the outside they are simply immutable objects.
The references you talk about are called, surprisingly, refs and they support full in-memory transactions. A simpler kind of a reference is atom, which fits your description 100%.
The whole Core API is devoted to elegant and epressive manipulation of these structures in a thread-safe, lock-free manner.
Yes. Lazy copying is easy to implement, but you would generally have to do it yourself.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Rule of thumb for choosing an implementation of a Java Collection?
I am looking for a 'Summary' list of all the Java collections detailing the pros and cons of each. I am particularly interested in things like
Which provide faster iteration
Which provide faster search
Which provide slower iteration
Which provide faster insertion or removal
I have seen some sites by searching on Google but i am looking for just a summary preferable in table format.
Thanks in advance.
Take a look at the Collections tutorial, particularly the section on implementations, which includes (in subsections) a discussion of performance characteristics for the various predefined classes. The collections framework is so large, I think, that it would be difficult to summarize everything in a single table.