wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question with sync Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question with sync" Watch "Thread question with sync" New topic
Author

Thread question with sync

Michael Michalakopoulos
Greenhorn

Joined: Apr 15, 2005
Posts: 2
Hello everyone, I am new here, I am trying to study for this certificate and I am a little bit confused about this question from KM in the pgjc mock exam. I tried to find the answer to the question in this forum, but I couldn't. I don't know what I am missing...



Two correct answers:
a. Code fails to compile.
b. Separate threads can execute the up() method concurrently
c. Separate threads can execute the down() method concurrently
d. Separate threads can execute both the up() and down() concurrently
e. The assertion in the jump() method will not fail under any circumstances.

From the whizlabs mock exam the answer for this question is
c and d, but i cannot understand their justification:
Executing synchronized code does not guard against executing non-synchronized code concurrently


Help!!! and Thank you!!!
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The way the choices are worded, the only correct answer appears to be C. The explanation they provide explains why C is correct, but does not have anything to do with why D is correct. It looks like an error on the part of the mock exam's author.

The only interpretation of D that I can think of to make it true is this: one thread can execute up() while another thread executes down() at the same time. However, D is worded in such a way as to indicate that both B and C are correct, which tends to contradict the interpretation of D that makes it true. So either D is poorly worded or the answer key is wrong.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30


Hi Michael

The down() method is not synchronized atall hence it is not thread safe and can be executed by several threads at same time.

Also, there could be ONE (and only one) thread executing the up() method [Only one, because it is synchronized] However, at the same time there could be another thread executing the down() method [because down() method is NOT synchronized].

Hope this helps..

regards
Saurabh
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Lets review the answers:

a. Code fails to compile.
>>>>>>>>no compilation error here

b. Separate threads can execute the up() method concurrently
>>>not possible, as up() method is synchronized

c. Separate threads can execute the down() method concurrently
>>>true. down() method is not synchronized and is public.



d. Separate threads can execute both the up() and down() concurrently
>>>true. both are public methods, one is synchronized, other is not. synchronized would be run by only one thread. one or more threads could be running the non-synchronized method down.


e. The assertion in the jump() method will not fail under any circumstances.

>>>will fail. after jump has started running, if down() is run by a different thread, assertion would fail
Michael Michalakopoulos
Greenhorn

Joined: Apr 15, 2005
Posts: 2
Hello friends and thanks for your help.

Now the time has passed, the sun is up again, and my mind is clearer. I agree with you both, and also think that this is a bad example of wording.

I know what the synchronized keyword does, but as Joe said, there only seems to be a correct answer and the wording in the other one is confusing.

Saurabh, you are also right I think, the methods up and down can be executed at the same time by different threads, that is only one thread can execute up() and one or more threads can execute down().

At 3.00am this question seemed very confusing (I still believe it is a bad example of mock exams in the Internet).

Thanks again,
Michael.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Unfortunately there are many mock exam questions that suffer from the same sort of ambiguity. The review process for real exam questions is presumably far more rigorous than for most mock-exam questions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question with sync