could you please explain more detail:
Numbers 1 to 5 will be printed (some may be repeated or missed) with "N Done" anywhere in the sequence where N cannot be determined.
How could the numbers be repeated or missed there?
As I see the increment and output are inside synchronized method, so they have "happens before" relationships.
The explanation you've provided makes sense to me, and I see how the same number can be output by different threads, but when I ran this the threads were printed out of order thus:
How is it managing to print them in descening order? If thread A incrememented the value from say 2 to 3, then was taken out of running, and thread B ran incrementing the number further from 3 to 4, why would they not both then print 4? I don't understand how a number can be printed which is smaller than the preceeding value..
Do you guys know how this happens please?
Many thanks!
RobynBackhouse wrote:The explanation you've provided makes sense to me, and I see how the same number can be output by different threads, but when I ran this the threads were printed out of order thus:
How is it managing to print them in descening order? If thread A incrememented the value from say 2 to 3, then was taken out of running, and thread B ran incrementing the number further from 3 to 4, why would they not both then print 4? I don't understand how a number can be printed which is smaller than the preceeding value..
Do you guys know how this happens please?
Many thanks!
This is a very good question. This happens because of the way Java Memory Model works. On multi core systems, the state of a non-volatile shared variable which is not accessed from synchonized blocks, may not be visible to other threads immediately or even ever! Please google Java Memory Model to read more about this concept as it is not possible to explain all of it in this post but there are tons of articles on it.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
In this scenario we are accessing static field from a non static method although it is synchronized but since we are creating different objects (Thread) and starting the threads. it is possible t1 (Thread 1) say read threadcounter (say=0) and goes to sleep (Thread pool) and 2nd thread (t2) comes and read threadcounter (threadcounter=0) and increments it (threadcounter=1) and after this t1 again resumes then the value of threadcounter would be 1 hence not continuous 1 to 5.
You seem to have missed highlighting the other part of the sentence, "Numbers 1 to 5 will be printed (some may be repeated or missed) ..."
The explanation shows one scenario where the number 1 is missed.
So I don't see any contradiction.
-Paul.
If you like our products and services, please help us by posting your review here.
Just need to clarify on the following.
The answer says.. "Numbers 1 to 5 will be printed (some may be repeated or missed) with "N Done" anywhere in the sequence where N cannot be determined."
I answered... "Total of 5 numbers will be printed followed by "N Done" where N cannot be determined."
considering the following scenario as well.
Since the threadCounter variable is not defined volatile, the changes might not be visible to other threads. So there is a possibility to get 5 times the number 1 printed. Am I wrong? PLease explain.
If you access a variable from within a synchronized block, then volatile is not required. Synchronization guarantees that most recent update will be visible to accessor thread.
If you like our products and services, please help us by posting your review here.
No, that option says, 5 numbers and then N done also. So, 6 numbers is not the problem with this option. Please read the explanation carefully. It explains why it is wrong.
If you like our products and services, please help us by posting your review here.