Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question from Sun's Free Mock Test

 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Given:
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you started two Threads on two different objects.
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Karl Prenton
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone provide a link to Sun's free mock test as I'm having trouble finding it!

TIA
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Zito:
Can anyone provide a link to Sun's free mock test as I'm having trouble finding it!

TIA

Free Proficiency Assessments has a link to Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (CX-310-055)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vasu",
Please check your private messages regarding an important administrative matter.
-Ben
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is getID method ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic