About Question enthuware.ocpjp.v7.2.1366 :

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

Moderator: admin

Post Reply
alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

About Question enthuware.ocpjp.v7.2.1366 :

Post 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

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

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

Post by admin »

Did you read the explanation? It explains exactly the thing that you are asking!
If you like our products and services, please help us by posting your review here.

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

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

Post by alex »

Yeah, they don`t have shared resource.

Thanks

RobynBackhouse
Posts: 23
Joined: Sun Apr 14, 2013 10:37 am
Contact:

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

Post 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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

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

Post by tn1408 »

In this case, #4 is also correct. Am I right??

Thanks,

Tony,

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

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

Post by admin »

No, because #4 says 5 numbers will be printed. That could mean any 5 numbers, not necessarily 1 to 5.
If you like our products and services, please help us by posting your review here.

gupta.v21
Posts: 17
Joined: Wed Jan 07, 2015 11:33 pm
Contact:

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

Post 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 :)

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

teodorj
Posts: 33
Joined: Tue Jun 19, 2018 10:27 am
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

teodorj
Posts: 33
Joined: Tue Jun 19, 2018 10:27 am
Contact:

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

Post 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.

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

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

Post by admin »

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

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 61 guests