• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads, Synchronizing code.

 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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.







 
Chintu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
milan chovatiya
Greenhorn
Posts: 24
Java Oracle Python
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks milan

Now i got the picture
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic