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

Doubt In Thread Topic

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the meaning of this statement?

A thread acquiring the lock of a class to execute a static synchronized method,has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.

please tell me
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A thread acquiring the lock of a class to execute a static synchronized method,has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.



Hi Anil,

class DemoThread extends Thread{

public void run() {

}

public synchronized static void doStuff1() { //Lock of the class

}

public void doStuff2() {
synchronized(this) { //Lock of the instance of the class
....
}
}

public static void main(String... args) {

}

}

Inside the static method, there is no "this". So a thread acquiring lock on the static method has no bearing of the lock of the syncrhonized instance method. So while a thread executing a static synchronized method, it has no bearing on any thread acquiring the lock on any object of the class to execute a synchronized instance method.

Got it?

Thanks and Regards,
cmbhatt
[ March 31, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that means
if a thread got a lock on static Synchronized method.
other threads can execute the non static synchronized methods
is it right?

[ March 31, 2007: Message edited by: anil kumar ]
[ March 31, 2007: Message edited by: anil kumar ]
 
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
Hi,


Meanwhile try this code,



Do you come to any conclusion after executing this code?
"While a thread is executing static block of code, other threads still can access the synchronized instance methods, and why only synchronized all other methods too except static methods because there is one and only lock of the static method per class"


Thanks and Regards,
cmbhatt
 
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
Hi Anil,

After editing the post, you are correct. You corrected yourself.



Thanks and Regards,
cmbhatt
[ April 01, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
i got it.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

You got it what Chandra said.

Interesting point.

1)// Class Lock is Unique. If one thread obtains it. No other thread in that JVM can obtain that lock until THE thread releases it.

synchronized(Sample.class){
}

2)//Object lock may not be unique. There is a possibility at runtime that SampleObjectRef point to some other Object.

synchronized(SampleObjectRef){
}

Concentrate on threads.
All the Best.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Srinivasan

yes i got it.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic