It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads, Synchronizing code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads, Synchronizing code." Watch "Threads, Synchronizing code." New topic

Threads, Synchronizing code.

Harold Ndou
Ranch Hand

Joined: May 19, 2013
Posts: 34

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

Joined: May 08, 2013
Posts: 20
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

Joined: May 19, 2013
Posts: 34
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

Joined: Jul 17, 2013
Posts: 24

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...

pain is temporary pride is forever
Harold Ndou
Ranch Hand

Joined: May 19, 2013
Posts: 34
Thanks milan

Now i got the picture
I agree. Here's the link:
subject: Threads, Synchronizing code.
jQuery in Action, 3rd edition