• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in getId() METHOD

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I HAVE PROBLEM IN THE FOLLOWING CODE.. ABOUT HOW THE getId() method works:


And given these two fragments:
1. synchronized void move (long id){
2.void move(long id){

Question:When either fragment 1 or fragment 2 is inserted at line 7,which are true?
Answer:With fragment 1, the output could be 4 2 4 2
With fragment 2, the output could be 2 4 2 4


I want to know how the getId() method produces result of 4 , 2

Its from Kathy sierra book from Ch-9 threading
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you are missing the parens after "run". When I run it, I get

11
10
11
10

and

10
10
11
11

for the two signatures. This shows that the id isn't specifically 2 and 4. Since all the choices have those #s, you get to assume those are the ids that came with example. And the fact that I get a different order for the second one shows the order isn't pre-determined.

Can you elaborate about what specifically you aren't sure about?
 
aman bisht
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how will I know that which id belongs to particular thread?
 
Marshal
Posts: 28174
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't know which is which. If you need to know, then you can provide a name for the Threads in their constructors, and use the getName() method instead of the getId() method. But this is just one of those certification questions which throw goofy scenarios at you so you can learn about Java, right? So for this example I don't think you really need to know the IDs or names of the threads.
reply
    Bookmark Topic Watch Topic
  • New Topic