• 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

Thread doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic