• 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

Threads - sleep() & notify()

 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does notify() and notifyAll() have any effect on a thread that's sleeping(Thread.sleep())?
What are *all possible ways* that cause a thread to come out of its sleeping state?
Though these questions seem very basic, please don't mind answering them - I've got some issues understanding them.
Thanks in adv!
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton for your help, Megha!
Well, see the image here that made me ask the question:

[ April 28, 2007: Message edited by: Faisal Pasha ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to Megha,

Some code to pang with




Sorry, I am bit unsynchronized...
Faisal,
The diagram is quite interesting,
It wakes up my inquiring mind!!! ...
Working with your diagram!


Thanks,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When thread goes to timeed_sleep,blocked state...




Still sleep -> notified -> ready_to_run, little difficult to understand.
Trying to find the scenario...

Regards,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Megha,

Did you see the behavior of your code by removing synch from the run method
of the SleepingThread class.

As I know, if the sleep() method is called from the sync block, sleep() does not release the lock, at least until it wakes up and it is also not guaranteed that after waking it will immediately release the lock.

I think there is nothing to do with sleep(...); it is all about the sync block, no two threads can acquire the same sync block together is the known
fact; if the thread, in which sleep(...) is executing, is acquiring the lock, how can notify be executed on the same object block.

Thread expert Henry Wong, is not available for so long.
He could help us to get the things clearly...


Anyways, Megha, your code gives some idea to me! (Thanks for that)


Regards,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly Megha, That is what my just above EDITED post talks about. I am thinking on the same issue. Please refer to that!!!



You see the output, by making Thread.sleep() call inside the sync block that you have placed in the main() method. I think it would be appropriate to think with sleep()...

And make the run method synchronized too (as you did in original code).

Thanks,
cmbhatt

[ April 28, 2007: Message edited by: Chandra Bhatt ]
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but I played with your code! The modified code is below:



Now I am going to sleep...
I know you can't notify me, because I am having the object lock. When I wake up, I will care to release that.


Thanks,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Herz the link to image:
http://www.bpurcell.org/blog/images/934/threads2.gif
If you right-click the image and select its properties you can see the link. This info could help you at JavaRanch in getting the links to images that are posted in the forums.
Well, herz the link to the page that hosts the above image:
http://www.bpurcell.org/blog/index.cfm?mode=entry&ENTRY=934
I think therz something wrong in the image. Itz not correct that a sleeping thread can be waken up by notify() etc. Usually, threads sleep to give another threads of same priority a chance to execute. Whereas, threads use notify() and notifyAll() in inter-thread interaction. The threads that are sleeping MUST sleep for the amount of time specified. In case some one disturbs them it would be an "interruption" and immediately the sleeping threads throw an exception(checked) while coming out of the sleep. And if none has disturbed their sleep, they will certainly wake up after the prescribed duration of sleep is finished. In which case, they move to runnable state and not running state. That's all about Thread.sleep(). Coming to notify(), its used to communicate/send message to the thread that's waiting for some object's lock. Because itz essential. Usually such inter thread communication takes place only in case of object lock. The thread that intentionally gives up its lock only to get it later uses wait() to do such. After invoking wait() it simply goes into Object's lock waiting pool(treat this as a separate state). Upon notified about the lock it still remains in the object's lock pool because some another thread might have also got the lock! Well, after getting the required lock, it moves into runnbale state where it still has to wait for the thread scedulers' mercy to get selected to run/execute.
So, the conclusion is, notify() has nothing to do with sleep(). If you need a proof, write some code and check it yourself.
[ April 28, 2007: Message edited by: Faisal Pasha ]
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Megha!
I just saw your post:

"Godd! you are rude.. anyways thanks"



Well, Im a very great admirer of Australian cricket. :-)
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems Megha has deleted all her replies in this thread!?! But, why?
They were really good providing some useful info.
Well, Megha, could you please post them back? Thanks a lot in adv!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic