• 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

how to release the lock??

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are there some other ways for a thread release the lock of a object except call wait() method??
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sun there are. (Sun as in Sun LiWei, not as in Sun Microsystems... ).
Consider the following piece of code:

Imagine that the myMonitor object is not locked by any thread and a thread (let's call it T) attempts to execute the above piece of code. Since the monitor is unlocked, T is able to get hold of it (locks it) and starts executing the code within the synchronized block. As soon as T exits the synchronized block, it releases the lock it holds on myMonitor; thereafter, myMonitor is free of locks and available for locking by another thread that can manage to get hold of it.
The difference this scenario has with an invocation of wait() is that a thread invoking wait() on a monitor, would result in the thread being added to the wait set of the monitor, and a subsequent invocation of notify() would result in the thread moving back to a Ready state and competing for locking the object. (don't forget that wait() HAS to be invoked within a synchronized block). On the other hand, when a thread exits a synchronized block it (1) releases the lock and (2) gets out of the competition for the particular monitor altogether.
Hope this helps,
Panagiotis.
SCJP2, January 2001 (91%)
IBM Certified Solution Developer VAJ, May 2001
(After that I went on a "certification sabbatical" of sorts )
[ September 12, 2002: Message edited by: Panagiotis Varlagas ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The monitor is also released if the method completes abruptely.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yield() puts back the running thread to ready state and wait() puts the thread in the waiting pool of the monitor.both are allowing other threads to execute.then why 2 methods to do the same thing?
 
pree sree
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yield() puts back the running thread to ready state and wait() puts the thread in the waiting pool of the monitor.both are allowing other threads to execute.then why 2 methods to do the same thing?
is the thread being blocked same as wait()?
[ September 13, 2002: Message edited by: pree sree ]
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait() will help in co-ordinating access of shared resources between threads running parallelly. It should be used along with notify()/notifyAll() to achieve this. This is its main objective.
But yield() will just put the thread back into the ready queue. It won't help in synchronising access to a shared resource.
Both do prevent the current thread from continuing to run, but each has a different purpose. In the case of wait() this is a side-effect. But in the case of yield(), it is the main purpose itself.
Again do note that yield() is not guaranteed to allow other threads to execute. Its behaviour depends a lot on the threading model of the operating system.
As Marcus rightly reminds us "Garbage collection and threading are 2 areas where java is platform dependent"
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're holding a lock, yield() does not release it.
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic