File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question from Sun's Free Mock Test 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 from Sun Watch "Thread Question from Sun New topic

Thread Question from Sun's Free Mock Test

Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144

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.
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 am having a little confusion on this question. Without running the code, my answer is C because I see that locked() is synchronized so only a or b will have access at a time.

The answer is B. Could someone please explain this?

vasu chowdary
Ranch Hand

Joined: Mar 15, 2008
Posts: 90
I think you started two Threads on two different objects.
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100

yes you started two threads, on two different objects. so they are not interfering in each other execution. everything here depends on how thread scheduler will delegate threads for execution

things would be different if you had this:

here you have started two threads on the same object. so they would be synchronized.
and please take a second before you post and use code tags.

<a href="" target="_blank" rel="nofollow">Use Real Words</a> <a href="" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Sorry to bother you guys. But I have a basic question. How did you guys identify that value of Thread ID would be either 6 or 7 or 8?

The Best way to predict your future is to create it - Every great individual common man
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
Can anyone provide a link to Sun's free mock test as I'm having trouble finding it!

Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
Originally posted by Frank Zito:
Can anyone provide a link to Sun's free mock test as I'm having trouble finding it!


Free Proficiency Assessments has a link to Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (CX-310-055)
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Please check your private messages regarding an important administrative matter.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Where is getID method ?

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
I agree. Here's the link:
subject: Thread Question from Sun's Free Mock Test
jQuery in Action, 3rd edition