• 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

Lock

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

The following question confuses me because of the words
"java keyword or method "
I know that "synchronized" keyword is used to lock an object.
But is "sleep" method is also true, bcoz it keeps its lock while in its sleep???
Q) What java keyword or method is used to lock an object in a monitor
a) lock
b) sleep
c) yield
d) synchronized
e) final
f) wait
Thanks
Aruna
[This message has been edited by Aru Ven (edited December 18, 2000).]
 
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear ven,
yeild()is a static method declared in the Thread class,which relinquishes the runnable state when called upon and goes to the ready state.Its always called on the currently executing thread whereas synchronized is a keyword,means it can,t be used as a identifier,its is used to prevent two threds from acessing an object data simultaneously and helps in providing the lock mechanism.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rishi :
Does calling yield on the currently executing thread releases the lock (I assume a situation where a thread currently holds the lock and then yield is called on that thread.) ?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does calling yield on the currently executing thread releases the lock


No, yeild does not release the lock on the object.
Bill
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The method which can lock is synchronised only..rishi if im right calling yield() will make the thread will give its lock to another thread of the same priority ao higher.
Vandanam
vijay
 
Rajiv Ranjan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads are always confusing.....
So Bill , if yield()does not release the lock, what happens if yield() is called on an executing thread and a thread of higher priortiy is waiting for CPU.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajiv, that is what yeild does, it moves a thread of equal or higher priority to run, but it doesn't have anything to do with locks. So if you have a lock on an object, and call yeild, nothing happens because it won't release the lock. You need to use wait() and notify() or notifyAll() if you want to work with locks and you use yeild if you are not working with locks.
I will try to get an example together to prove my point.
Bill
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright here is a simple examle:
First I do not have the synchronized keyword in there so that there are no locks:

When yeild is called, the thread will switch back and forth printing out 0 then 1 and then 0 and 1 again until both loops are done. So in this case, the active thread yields for the other thread.
Now let's change it so that the method aMethod() is synchronzied.

This time, thread 0 starts, gets the lock and goes until the loop is finished and then thread 1 starts. So the lock never gets released with the call to yield.
Hope that helps
Bill
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry .... What I meant was "sleep" method.... Bcoz "sleep" holds the object monitor.
So in the above Question Can the answer be
"synchronized" & "sleep" ?
Aruna
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, sleep doesn't lock the object either. sleep() makes the currently active thread stop for a bit, but it doesn't release the lock.
 
Rajiv Ranjan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill,
Your examples are excellent.
Rajiv
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem Rajiv. I appreciate your feedback.
bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic