• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Threads & Locks...

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
I am confused with the term "lock on a Object"
According to Java,
1) "Every object has a lock. At any given time the lock is controlled by
atmost one Thread.
2) "A Single Thread can obtain locks on many object &/or Multiple locks on
the same object"
I find the above statements contradicting specially the second statement "
"A single thread....on the same Object"
Using below synchronised method i need to know
"How many objects has the thread obtained locks on ?"
public class MyClass implements Runnable
{
Thread t;
MyClass()
{
t = new Thread(this);
t.start()
}
public void run()
{
Object x1 = new Object();
Object x2 = new Object();
smeth(x1,x2);
}
synchronized void smeth(Object x,Object y)
{
Object k = new Object();
//
}
Can v say that Thread has obtained lock on 4 objects, 2 being from parameters
1 from the object "this" which invoked the smeth() & one on local Object k.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume we have the following main:
public void static main (String[] args) {
MyClass mc = new MyClass() ;
}
Then mc.t would obtain mc's lock when mc.t enters the smeth() method, and relinquishes the lock when mc.t exits the method.
mc.t obtains one lock (mc's).
Remember that
synchronized void smeth(Object x, Object y) {
////
}
is equivalent to
void smeth(Object x, Object y){
synchronized(this){
//// some code
}
}
x and y are irrelevant to the locking process. They would only be involved if there was some code like so:
synchronized(x){
/// some code
}
synchronized(y) {
/// some code
}
David
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
An object have only one lock, A Thread may acquire many locks on different objects.
Regards denish
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic