This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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.
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].
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
Joined: Apr 15, 2005
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.
Joined: Apr 10, 2005
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.