Page 1 of 1

About Question enthuware.ocpjp.v8.2.1558 :

Posted: Thu May 26, 2016 7:51 pm
by betinhoneres
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?

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

Posted: Thu May 26, 2016 10:39 pm
by admin
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.

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

Posted: Sun Apr 01, 2018 1:18 am
by shamran99
Hi,
Is there any possibility to get the final value of the variable threadCounter greater than 10?

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

Posted: Sun Apr 01, 2018 1:54 am
by admin
No, that is not possible.

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

Posted: Fri Apr 13, 2018 6:17 am
by shamran99
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

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

Posted: Fri Apr 13, 2018 9:23 pm
by admin
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.

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

Posted: Tue Mar 05, 2019 2:48 pm
by crazymind
Hi, I am kinda surprise that no one ask how main method access this synchronized block. It first checks whether main thread is TestClass or not. Basically, each loop same main thread will enter the synchronized block and create a new Thread; therefore, it prints 10 numbers but which number will be printed and the order of these 10 number can not be determined. I hope my logic is correct. Please verify, thanks.

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

Posted: Tue Mar 05, 2019 10:07 pm
by admin
Yes, you got it right.
The main thread acquires the lock of TestClass.class object in every iteration. (A thread is allowed to reacquire the same lock any number of times.) In each iteration, it creates and starts a new thread.

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

Posted: Sat Dec 07, 2019 5:12 pm
by Bhaskar
I am a bit confused about the role of synchronized block over here. What i have understood is, the main thread acquires the lock on TestClass.class, spawns a new thread from within it and exits the block. It does not wait for the spawned thread to complete. If that would have been the case, the results would have been ordered and it would defy the multi-threading concept in this context. Please verify if my understating is correct, thanks.

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

Posted: Mon Dec 09, 2019 10:14 pm
by admin
Correct.