File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread doubt?

 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

wait(2000);

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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
or
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.


Yours,
Bu.
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bu,

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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic