• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

static synchronized

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When there is a thread executing an object's static synchronized method, will it prevent other threads from executing the same object's instance synchronized method?
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you mean a different, non-static method other than the static one being executed (but still a member of the same object), attempted to be executed by another thread simultaneously?
If my understanding is correct, then I say no, it will not prevent the instance method from being executed by other threads.
Even though both methods are synchronized within the same object, they are totally separate from each other.
At least that's my best guestimate...
If anyone could confirm this for me, I would be most humbly appreciative.

------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static Methods are associated with classes and instance methods with Objects. If a thread wants to execute a static synchronized method, he will have to get the class lock and if it wants to execute an instance synchronized method, it will have to obtain the object lock. They are separate locks and a thread can go can execute both methods provided he gets required locks. Hope it clears to some extent.... Regards,
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a thread is executing a static synchronized method of a class it has to acquire class level lock of that object.If one thread acquires a class level lock of an object then other threads can not run any synchronized methods of that object so long as the thread acquiring the class level lock releases the lock.
 
Mudassar Shafique
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried this thing by writing the following program. The output of this program is showing that both the threads are taking turns. One thread(t1) is executing static method of the class and other(t2) is executing an instance method of object of the same class, so If they can take turns it means class level lock is independent of obejct level lock, aint?
public class javaranch {
public static void main(String[] args){
Thread t1 = new Thread(new th1());
t1.start();
Thread t2 = new Thread(new th2());
t2.start();
}

}
class th1 implements Runnable{
public void run(){
common.classMethod();
}
}
class th2 implements Runnable{
public void run(){
common commonObject = new common();
commonObject.iMethod();
}
}
class common{
public static synchronized void classMethod(){
while(true){
System.out.println(Thread.currentThread().getName() + " In static sync method");
}
}
public void iMethod(){
while(true){
System.out.println(Thread.currentThread().getName() + " In instance sync method");
}
}
}
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic