About Question enthuware.ocpjp.v8.2.1558 :

Moderator: admin

Post Reply
betinhoneres
Posts: 1
Joined: Sat Apr 30, 2016 4:10 am
Contact:

About Question enthuware.ocpjp.v8.2.1558 :

Post by betinhoneres » Thu May 26, 2016 7:51 pm

Hello!

The variable "threadcounter" is static, then your value is only one. The final instruction of explanations is wrong:
"It is, therefore, possible that each of the ten threads see its value as 0 and updated it to 1. Thus, one possible outcome could be that 1 is printed 10 times!"
When of all threads executes the method run, even if the same time, the variable "threadcounter" will be increased. Right or not?

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

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

Post by admin » Thu May 26, 2016 10:39 pm

No, the explanation is correct. As explained in the explanation, threadcounter is not volatile and none of the threads are accessing the variable in a synchronized block, so one thread may change its value from 0 to 1 but another thread may still see 0.

kakawi
Posts: 8
Joined: Sat Aug 12, 2017 1:43 pm
Contact:

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

Post by kakawi » Sat Aug 12, 2017 3:12 pm

Futhermore, even if threadcounter was volatile it doesn't help to see all 10 numbers
The reason is:
increment is not atomic operation, so two or more threads can read the same (even volatile) variable and rewrite end value

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

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

Post by admin » Sun Aug 13, 2017 2:13 am

That is correct. This has been added to the explanation.

thank you for your feedback!
Paul.

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

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

Post by shamran99 » Sun Apr 01, 2018 1:18 am

Hi,
Is there any possibility to get the final value of the variable threadCounter greater than 10?

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

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

Post by admin » Sun Apr 01, 2018 1:54 am

No, that is not possible.

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

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

Post by shamran99 » Fri Apr 13, 2018 6:17 am

Hi,

Thanks for the reply. Im confused with the statement about volatile and atomic. Let say the increment operation for threadCounter is atomic but the variable is not declared volatile. In such scenario, can we say that the final value of threadCounter will be always 10?

Regards,
Shamran

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

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

Post by admin » Fri Apr 13, 2018 9:23 pm

First, you need to realize that Java does not have any atomic increment operator. What Java has is an AtomicInteger class, which supports methods to increment/decrement the value inside an object of this class atomically.

Second, you need to realize that int i and AtomicInteger ai are two entirely different kind of things. int is a primitive while AtomicInteger is a class. So when you do i++, you are changing the value of the variable i. To make this new value visible to other threads, you need to make i volatile. This part has nothing to do with atomicity.

When you call ai.incrementAndGet(), you don't change the value of ai. ai still keeps pointing to the same AtomicInteger object as before. This is the fundamental difference between a primitive and a reference. So there is no need for ai to be declared volatile here because ai doesn't change. The integer value that is kept inside the AtomicInteger object is incremented and it is incremented atomically so every threads gets to see the incremented value of its integer value correctly and no updates are lost.

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests