• 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

Threads, Synchronizing code.

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


And given these two fragments:
I. synchronized void move(long id) {
II. void move(long id) {
The answer is C and E:

C. With fragments I, the output could be 4 2 4 2
E. With fragment II, the output could be 2 4 2 4


Hi everyone
I am having tough time in understanding the above output. I want to know how do we get these id's : 4 2 4 2 and 2 4 2 4

I got this code from K & B SCJP 6 Studyguide, chapter 9, self test questions, question 17

I wil real appreciate your assistance.







 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two threads are running on different instances. So you cannot predict the output. The answers you have given will not be always true.
 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is how do we get this numbers, How do i know the ID of the current tread .ie 2 4 2 4
 
Greenhorn
Posts: 24
Python Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think its random id ..which you are getting
you have to concentrate on synchronized word rather than id values....and one thread will not have same id in 1st n 2nd call...
 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks milan

Now i got the picture
 
reply
    Bookmark Topic Watch Topic
  • New Topic