About Question enthuware.ocpjp.v8.2.1083 :

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

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1083 :

Post by schchen2000 »

What were you trying to say when said
ai.set(ai.get()+1);

This is wrong because between the time when ai.get() returns a value and ai.set() is called, another thread may call increment and may get to execute ai.set with the same value. This will make you lose one increment.
?

Please be generous with your explanation.

The intention of your explanation should be helping people understand the concepts via your words.

Your purpose is not to save words.

You have very good materials and I commend your effort to come up with all these materials.

I greatly benefited from your materials for my Oracle Java Associate exam.

I'm not being bitchy. I just want you to get better and to get respect that you very much deserve for the work and the effort you put in.

If you have a tough time conveying your message, no one would understand you no matter how brilliant your work is.

Schmichael

P. S.

If saying too much does not get your point across, a couple of lines of code would do.

ai.set(ai.get()+1); // This is what's given.

For example,

Variable ai has a value of 5 originally.

ai.get() will return 5.

Before ai.set(5 + 1) happens,

"another thread may call increment and may get to execute ai.set with the same value."

I understand why another thread can come in between ai.get() and ai.set(5 + 1) because we are between 2 independent atomic operations.

"....and may get to execute ai.set with the same value." This part is very poorly written.

This was where I got stuck.

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

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by admin »

Point taken, schchen2000. We will work on it and make the explanations better.


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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by schchen2000 »

admin wrote:Point taken, schchen2000. We will work on it and make the explanations better.

thank you!
-Paul.
Thanks, Paul. I wasn't trying to be mean or anything.

I, however, still like to know what you were trying to explain though. Thanks, Paul.

Schmichael

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

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by admin »

Let's say the CPU executes two threads in the following sequence:

1. Thread 1 : executes ai.get() and gets a value of 0, which is kept in the threads local register
2. Thread 2 : executes ai.get() and gets a value of 0, which is kept in the threads local register
3. Thread 1 : executes previously retrieved value +1 i.e. 1
4. Thread 2 : executes previously retrieved value +1 i.e. 1
5. Thread 1 : executes ai.set(1), so ai is now set to 1
6. Thread 2 : executes ai.set(1), so ai is again set to 1

OR

1. Thread 1 : executes ai.get() and gets a value of 0, which is kept in the threads local register
2. Thread 2 : executes ai.get() and gets a value of 0, which is kept in the threads local register
3. Thread 1 : executes previously retrieved value +1 i.e. 1 and is about to pass this value to ai.set
4. Thread 2 : executes previously retrieved value +1 i.e. 1
5. Thread 2 : executes ai.set(1), so ai is set to 1
6. Thread 1 : executes ai.set(1), so ai is again set to 1




You can see that ai should have been 2 but we lost one increment because the CPU execute another thread in between the ai.get() and ai.set() calls of thread 1.

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by schchen2000 »

admin wrote:Let's say the CPU executes two threads in the following sequence:

1. Thread 1 : executes ai.get() and gets a value of 0, which is kept in the threads local register
2. Thread 2 : executes ai.get() and gets a value of 0, which is kept in the threads local register
3. Thread 1 : executes previously retrieved value +1 i.e. 1
4. Thread 2 : executes previously retrieved value +1 i.e. 1
5. Thread 1 : executes ai.set(1), so ai is now set to 1
6. Thread 2 : executes ai.set(1), so ai is again set to 1

OR

1. Thread 1 : executes ai.get() and gets a value of 0, which is kept in the threads local register
2. Thread 2 : executes ai.get() and gets a value of 0, which is kept in the threads local register
3. Thread 1 : executes previously retrieved value +1 i.e. 1 and is about to pass this value to ai.set
4. Thread 2 : executes previously retrieved value +1 i.e. 1
5. Thread 2 : executes ai.set(1), so ai is set to 1
6. Thread 1 : executes ai.set(1), so ai is again set to 1




You can see that ai should have been 2 but we lost one increment because the CPU execute another thread in between the ai.get() and ai.set() calls of thread 1.

HTH,
Paul.
Both get() and set() are atomic. There is also a method called incrementAndGet(), which is also atomic. In fact, incrementAndGet() would have been a better choice to accomplish what we've set out to do.

The original problem we were facing was not being able to make the real-time value available to the next operation.

Is that correct? Thanks a lot, Paul for your time and explanation.

Schmichael

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

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by admin »

Yes, you got it right.
If you like our products and services, please help us by posting your review here.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by schchen2000 »

admin wrote:Yes, you got it right.
Thanks a lot. Much appreciate it.

Schmichael

RogierA
Posts: 4
Joined: Fri Oct 16, 2020 4:15 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by RogierA »

Why wil the methods that also return a value not work?
for example, incrementAndGet();
does it not increment the value if you do nothing with its return value?

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

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by admin »

This issue is with calling two different methods one after another. Even if each individual method call is atomic, the chain of calls is not.
I am not sure what you mean by "Why wil the methods that also return a value not work?" In what situation will it not work? Please post some code that shows what you mean so that we can provide an explanation.
If you like our products and services, please help us by posting your review here.

RogierA
Posts: 4
Joined: Fri Oct 16, 2020 4:15 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by RogierA »

admin wrote:
Fri Oct 16, 2020 4:32 am
This issue is with calling two different methods one after another. Even if each individual method call is atomic, the chain of calls is not.
I am not sure what you mean by "Why wil the methods that also return a value not work?" In what situation will it not work? Please post some code that shows what you mean so that we can provide an explanation.
Sorry, i got it backwards, the correct answers were the ones that do return a value.
I meant to ask why the methods that do not return a value (like ai.increment();) are incorrect answers

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

Re: About Question enthuware.ocpjp.v8.2.1083 :

Post by admin »

Because AtomicInteger does not have any method named increment(). It has getAndIncrement() and incrementAndGet().
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 26 guests