Page 1 of 1

About Question enthuware.ocpjp.v7.2.1366 :

Posted: Mon Mar 25, 2013 3:23 pm
by alex
Hi,

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.

Code: Select all

public synchronized void run()    
{       
threadcounter++;       
System.out.println(threadcounter);    
}
So there is no interference.

Regards,
Alex

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

Posted: Mon Mar 25, 2013 3:30 pm
by admin
Did you read the explanation? It explains exactly the thing that you are asking!

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

Posted: Mon Mar 25, 2013 3:45 pm
by alex
Yeah, they don`t have shared resource.

Thanks

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

Posted: Sun Apr 28, 2013 2:26 pm
by RobynBackhouse
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:

Code: Select all

1
4
3
2
4 DONE
5
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!
:D

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

Posted: Thu May 02, 2013 8:39 am
by admin
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:

Code: Select all

1
4
3
2
4 DONE
5
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!
:D
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.

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

Posted: Thu Apr 03, 2014 12:50 pm
by tn1408
In this case, #4 is also correct. Am I right??

Thanks,

Tony,

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

Posted: Thu Apr 03, 2014 8:50 pm
by admin
No, because #4 says 5 numbers will be printed. That could mean any 5 numbers, not necessarily 1 to 5.

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

Posted: Fri Jan 23, 2015 8:04 pm
by gupta.v21
Paul,

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.

Vivek Gupta :)

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

Posted: Sun Jul 02, 2017 11:38 am
by ssszzz
Answer:
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.
Explanation:
So both the threads print 2 and 1 is never printed.
there is some contradiction here, isn't?

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

Posted: Sun Jul 02, 2017 10:53 pm
by admin
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.

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

Posted: Thu Apr 26, 2018 5:44 am
by shamran99
Hi,

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.

Regards,
Shamran.

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

Posted: Thu Apr 26, 2018 9:44 am
by admin
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.

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

Posted: Sat Feb 22, 2020 1:42 pm
by teodorj
Option 4 is wrong because of the line in the main method..

System.out.println(threadcounter+" DONE");

any numbers (1-5) will be printed (from run) then the main method will print the thread counter value .
So it is not equal to 5 numbers

Am I correct?

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

Posted: Sat Feb 22, 2020 5:25 pm
by admin
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.

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

Posted: Mon Feb 24, 2020 8:58 am
by teodorj
Option 4 is also not correct because of the bold part..
Total of 5 numbers will be printed followed by "N Done" where N cannot be determined.

This is wrong because the order in which the print method in main cannot be determined.
Please verify.

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

Posted: Mon Feb 24, 2020 10:47 am
by admin
Correct.