Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Thread Question from Sun's Free Mock Test

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you started two Threads on two different objects.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone provide a link to Sun's free mock test as I'm having trouble finding it!

TIA
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vasu",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is getID method ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic