Page 1 of 1

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Wed Jun 15, 2016 9:52 am
by dieterdde
Hello,

whilst I took the right answer for lack of any better answer, I'm really puzzled why it's the right answer, again it may be semantics but, I don't believe it's correct to state that add/remove operations can happen simulteanously even if you toss in the word "safely" in there, doesn't mean it's simultaneous. Reason: CopyOnWriteArrayList will block simultaneous write attempts (uses a ReentrantLock internally), without blocking reads (which is it's main advantage over a ReentrantReadWriteLock).

It would be right to say that multiple threads can "attempt" simultaneous add/remove operations, but they don't actually "happen" simultaneous, right?

This question drove me nuts, I finished the test with 10 minutes to spare and spent it in full on this question, as none of the answers were truly correction my opinion. The 3rd potential answer got it right that you can't add simultaneously, but said "can't do it in a thread safe manner" which is also not correct as CopyOnWriteArrayList is of course thread safe.

As you can tell from my previous posts, I REALLY struggle sometimes with wordings! Is the exam also as tricky like that in wording? It's gonna be a nightmare for me.

Luckily it was the only question that drove me nuts, so I did get 100% on the test 11 on Concurrency. Phew. Onto chapter 12 on Localization!

Cheers

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Wed Jun 15, 2016 10:27 am
by admin
I am not sure what is your reference point when you think of the word simultaneous. If you look at it from a CPU level, nothing is simultaneous in a single core machine. After all, the CPU can execute only one instruction at a time.
But if you look at it from the OS level, you can indeed say that there are multiple threads that are doing something simultaneously.

In this question and also in general, when you talking about simultaneous execution of threads, you talk from the JVM's perspective. It manages multiple threads and executes them simultaneously (now, whether it is an illusion using time slicing or real concurrent execution is a different issue.) So here, it is ok to say multiple threads can read and write to the data structure simultaneously. All it means is that one thread doesn't have to synchronize its operation with another thread before modifying the shared data structure. If multiple threads don't synchronize, then obviously, you are accepting the possibility that they can both try to modify it at the same time.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Wed Jan 11, 2017 2:23 pm
by jagoneye
It would be helpful if provided which Collection allows null key/value insertions and which not including those in the concurrent package as well.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Aug 05, 2018 5:47 am
by nowkarol
So here, it is ok to say multiple threads can read and write to the data structure simultaneously
I think no. If yes then we can say that Collections.synchronizedList allow to write and read data simultaneously. It is internally synchronized. Same as CopyOnWriteArrayList - it is internally synchronized while copying internal array. So two threads are not allowed to modify it in same time. But critical section is narrowed only to copying array operation.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Aug 05, 2018 6:10 am
by admin
You are taking a statement out of context. Please read the whole post in its entirely. "simultaneous access" always depends on the perspective. From the application perspective everything may happen simultaneously while from a CPU perspective everything happens sequentially. Now, in the middle these two ends, some parts may appear to happen simultaneously while some may not. That depends on which side of the things you are on.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Jan 05, 2020 9:13 am
by Bhaskar
I am trying to understand how multiple threads mutate the collection simultaneously. If thread T1 is adding something to the collection and at the same time another thread T2 also tries to add something, will it have to wait for T1 to complete or it creates it own copy of the collection and both of them are merged at a later stage? If that's the case then the word "simultaneous" makes sense. Please explain.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Jan 05, 2020 11:00 am
by admin
How a collection provides simultaneous access depends on how it is implemented. You can take a look at the source code. Vector, for example, uses synchronized methods, while new concurrent collections use synchronized blocks. You could write your own implementation that does it through merge. Though, in any implementation, there will always be some part of code that will need to be synchronized.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Jan 05, 2020 11:42 am
by Bhaskar
Sorry i should have been more specific when i said "collection". I was specifically asking about CopyOnWriteArrayList. Please explain the question with regards to CopyOnWriteArrayList.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Jan 05, 2020 11:55 am
by admin
I am not really sure what exactly are you trying to get at. Can you tell me which part of the problem statement, option, or the explanation is not clear so that I can elaborate on it?

As far as "simultaneously" is concerned, it is used in the same sense as it is used all the time. You have two threads who are trying to manipulate a collection simultaneously. A regular ArrayList may get corrupt while with CopyOnWriteArrayList will not.

As far as, how CopyOnWriteArrayList achieves this, the JavaDoc for this class says that it creates a copy of the underlying array.
You can go through its source code if you want to know exactly how it does this.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Jan 05, 2020 2:28 pm
by Bhaskar
Thanks i have understood the crux. While we are still on this topic, can you please explain why doesn't below code throw cme?

Code: Select all

	List al = new ArrayList();
        al.add("A");
        al.add("B");
        Iterator it = al.iterator();
        while (it.hasNext()) {
            String hh = (String) it.next();
            al.remove("A");//1
        }
        System.out.println(al); //[B]
        


If i write al.add("A"); at //1, it throws a cme. Both add and remove are modifying the collection, shouldn't they both throw cme?

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Sun Jan 05, 2020 5:50 pm
by admin
CME is a possibility, not a certainty, when doing modification. It depends on how the code is written.

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Mon Feb 17, 2020 11:58 am
by jackdaniels
Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException
set and add methods are not part of an Iterator anyway so only remove can be used?

https://docs.oracle.com/en/java/javase/ ... rator.html

Re: About Question enthuware.ocpjp.v7.2.1087 :

Posted: Mon Feb 17, 2020 7:26 pm
by admin
Right.