wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A Question about Thread.Need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A Question about Thread.Need Help" Watch "A Question about Thread.Need Help" New topic
Author

A Question about Thread.Need Help

Andrew Cheng
Greenhorn

Joined: Jan 09, 2002
Posts: 12

Under which conditions will check() return true when called from adifferent class?
A. check() can never return true
B. check()can return true when setXY is called by multiple threads
C. check()can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SynchTest is changed to allow x and y to be set separately.
The answer is C.
I am not clear about the question.Please explain in detail.Thank in advance!
:roll:
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Andrew Cheng:

Under which conditions will check() return true when called from adifferent class?
A. check() can never return true
B. check()can return true when setXY is called by multiple threads
C. check()can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SynchTest is changed to allow x and y to be set separately.
The answer is C.
I am not clear about the question.Please explain in detail.Thank in advance!
:roll:

Hi Andrew
Well the condition in the check method is X != Y. So if you use the answer B that means you have synchronized the threads and that both of the values are going to be equal. X!=Y is going to return false, while if you call setX & setY seperately you are not synchronizing the behavior so any thread can call any method with any value and the check X!=Y is going to return true, SO the answer C is right setX & setY called seperately will return true.
Hope this helps
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
ok let's see.
check will return true only if x and y are differnt .
in order for them to be different we need to put diffreent values in both of them using the functions given.
answer a is wrong as we will see later (cause it can return true).
answer b is wrong cause if the function setXY is called by multiple threads (with diffrents i all the time) it wont matter for the varaiables. this method is synchronized - meaning only 1 thread will be allowed in it at any given time so it will change the values of x and y together.
notice that the check mehtod is also synchronized - maining that only after 1 thread changed the values of both x and y to the same i ,
another thread will be able to check their status and see that they are equal thus false.
however in answer c is correct: because the setx and sety method are not synchronized different threads will be able to change them with different values simulatnatsly and check method can return true.
we already did what answer d suggested and allowed to change the x and y seperatly.
i know its messy but i hope u got it.
Phoebe Song
Ranch Hand

Joined: Jan 17, 2002
Posts: 54
What about D? I think it is also correct?
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
like i said:

we already did what answer d suggested and allowed to change the x and y seperatly .

d suggests we change the program to make x and y changes seperatly - which is a good idea BUT it is already possible in this program.
 
Don't get me started about those stupid light bulbs.
 
subject: A Question about Thread.Need Help
 
Similar Threads
Question on threads
Qn on Threads
Multi Thread Question
confuse on the answer