• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Thread

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
current thread get a lock on the object when it executes a synchronized method.the lock will be released when the thread executes wait() method or it returns the method.
my doubt is:
whether the lock will be released when the thread executes yeild() or other methods that cause the thread to change from the running state???

second doubt:
when one thread calls sleep() method, will thread shedular allow another thread to execute???
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.3/docs/api/index.html
See the above documentation.
The sleep does not loose ownership of any monitors.
A call to yield causes the currently executing thread to move to the Ready state if the scheduler is willing to run any other thread in place of the yielding thread. I wrote a small piece of code and put "yield" inside of synchronized code. It never gave up the monitor. At the same time I had another piece of synchronized code waiting for the object. It never ran.
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boone in java2 exam guide in page 300 stating that
"the lock can be released when a synchronized method executes wait(),yeild(),or other methods that cause the threda to change from running state."
is this statement wrong???
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the method called wait() or yield() thread goes to Ready_to_run state (or waiting state after wait() call)that is it is not in the running state. So it has to release the lock.
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
some one can clear my doubt with a piece of code.
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
some one help me!!!
to clear my doubt with a piece of code.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeena,
From what I could gather from Khalid Mughal, the yield() method is used to relinquish processor resources (CPU). I do not think it will relase the lock on the monitor. This is more to do with processor resource sharing rather than object locks I believe.
So if a thread is waiting for an object lock and a second thread has the object lock, executing the yield() method in the second thread should not relase the object lock.
Somebody please correct me if wrong.
Thanks
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic