5. public class Lockdown implements Runnable { 6. public static void main(String[] args) { 7. new Thread(new Lockdown()).start(); 8. new Thread(new Lockdown()).start(); 9. } 10. public void run() { locked(Thread.currentThread().getId()); } 11. synchronized void locked(long id) { 12. System.out.print(id + "a "); 13. System.out.print(id + "b "); 14. } 15. } What is true about possible sets of output from this code? A. Set 6a 7a 7b 8a and set 7a 7b 8a 8b are both possible. B. Set 7a 7b 8a 8b and set 6a 7a 6b 7b are both possible. (Two different Lockdown objects are using the locked() method.) C. It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible. D. It could be set 7a 8a 7b 8b but set 6a 6b 7a 7b is NOT possible.
I pick up C. I don't know how its ANS is B. Anybody can explain it? Thanks. [ March 24, 2008: Message edited by: Ben Souther ]
Actually this question came from sun's sample questions for 310-065
Anybody who register and login in sun web site can get it but only 10 questions (all those things are totally free).
All those information are public to everybody legally, Make sure look posts very carefully. It is the "SAMPLE" questions "NOT REAL" questions. Just like all mock exam questions on the web. It's hard to understand why the question should be taken away.
Originally posted by liqiang yang: Actually this question came from sun's sample questions for 310-065
Anybody who register and login in sun web site can get it but only 10 questions (all those things are totally free).
All those information are public to everybody legally, Make sure look posts very carefully. It is the "SAMPLE" questions "NOT REAL" questions. Just like all mock exam questions on the web. It's hard to understand why the question should be taken away.
Thank you. -Ben
liqiang yang
Ranch Hand
Joined: Jan 20, 2008
Posts: 92
posted
0
I still stuck to this question. Can anyone can help? Thanks.
Because "synchronized void locked(long id)", so at the certain moment only one Thread can exclusively access locked method. Therefore, it's impossible to get output like: 6a 7a 6b 7b(Thread 7 enter locked() before Thread finished to print out "b") which means 2 Threads concurrently access locked method. Am I right?
Irina Goble
Ranch Hand
Joined: May 09, 2004
Posts: 75
posted
0
Originally posted by liqiang yang: I still stuck to this question. Can anyone can help? Thanks.
Liqiang, the answer is right in your first post.
(Two different Lockdown objects are using the locked() method.)
Explanation: 1.two thread objects created 2.method is synchronized but actually it does not affect two threads 3.each thread having its own lock both are diffrent objects 4.control goes sequentially therefore print order is always "a" and then "b" 5.b is not printed before a this is never possible
If I am wrong anywhere in explanation please notify me for that
I tried to go to the Sun Certification site and registered for a Student ID. I am currently logged in already but cannot find the link where the questions are. Could I ask you to please tell me where exactly the free sample questions can be taken for SCJP 5.0 & 6.0?
The "a" must be followed by "b". This is perfectly right for sure.
I just can't understand why those two threads (identified by its ID#, like 6, 7, 8 or 9 and so on) for example, how to translate this : 6a 7a 6b 7b
6a -> Thread6 print out "a", just locked's first line and not finished locked yet, Now Thread6 still hold the lock and try to execute second line of locked. 7a -> But at this point, Thread7 coming up to execute the first line of locked(print out the "a")
So Thread6 and Thread7 access the method locked which was marked as synchronized. Also I put this code on my IDE and make a big loop number to run it and the output can support my thoughts. This is why I am so confuse about this question.