This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static synchronized" Watch "static synchronized" New topic
Author

static synchronized

Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
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

Joined: Jan 24, 2001
Posts: 219
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


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Mudassar Shafique
Greenhorn

Joined: Mar 16, 2001
Posts: 10
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

Joined: Jun 22, 2000
Posts: 8
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

Joined: Mar 16, 2001
Posts: 10
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");
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static synchronized