This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static synchronized

 
Wong KK
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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?
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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
 
Mudassar Shafique
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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,
 
debasish tripathy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
  • 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");
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic