About Question enthuware.ocpjp.v7.2.1087 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
dieterdde
Posts: 19
Joined: Wed May 25, 2016 4:33 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post 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.

nowkarol
Posts: 5
Joined: Sun Aug 05, 2018 5:41 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

CME is a possibility, not a certainty, when doing modification. It depends on how the code is written.
If you like our products and services, please help us by posting your review here.

jackdaniels
Posts: 10
Joined: Sun Feb 04, 2018 7:05 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Right.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests