The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread doubt? 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 "Thread doubt?" Watch "Thread doubt?" New topic

Thread doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

Hi Ranchers,

The following question is from K&B book page 738,

Assume the following method is properly synchronized and called from a thread A on an object B


After calling this method, when will the thread A become a candidate to get another turn at the CPU?

Can anyone help me realize the above through code and I'm little bit scary at the bold portion above. If I can get an explanation in the form of a code that will really help me understand synchronization concept to its depth. Ranchers, please help me...

Thanks in advance for the help.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Jothi,

it means that thread A is in a synchronized block, that has been synchronized on object b.
It enters the waiting room of object b and waits there
a) until some other thread notifies one or all waiting threads in the waiting room of object b and so our thread.
b) 2000 milliseconds are over.

Here's an example code. It's a bit long, but shows the two possibilities.
In the part above the sleep in the main method a thread doesn't get notified, and in the part below a thread waits, but becomes notified and ends earlier.
The sleep in the main method only seperates the two versions. I don't provide the output. Try it to see the timing.


all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032


Thanks for the example. Your explanations are really helping me to understand. Particularily, your garbage collection diagram was really understandable and I can now easily tweak out questions related to gc. Hope that after trying out the above code, I will be comfortable with synching threads as well.

Thanks once again.
I agree. Here's the link:
subject: Thread doubt?
It's not a secret anymore!