About the static synchronized method
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rajesh_Chauhan
Posted On:   Monday, April 3, 2006 03:32 AM

Can some body tell what is difference between static synchronized method and non static synchronized method......
If one thread is executing static synchronized method then other thread can enter other non static synchronized method and non static non synchronized method.
Plz tell the whole picture of this one.....


Please use html tags to format code blocks.

Re: About the static synchronized method

Posted By:   Edward_Harned  
Posted On:   Monday, April 3, 2006 12:55 PM

public Class MyClass

public static synchronized a(){}

public static synchronized b()

public synchronized c()


When executing a static synchronized method, the lock is on the Class object (MyClass). Therefore, no other thread can execute any other static synchronized method since they all lock on the same class.


Only one thread at a time can execute an instance method that is synchronized from the same object.


MyClass mc = new MyClass();

mc.c()

only one thread can execute the above c() at a time.


MyClass.a()

Only one thread can execute the a() OR the b() at a time.

Re: About the static synchronized method

Posted By:   Almagest_FUTT  
Posted On:   Monday, April 3, 2006 07:36 AM

Synchronization is (partly) all about monitors. Each synchronization step is associated with a monitor. Monitors are thingies like very small toilets only one person can access at a time.

Static synchronized methods have their monitor associated with the class itself. That's not fully precise but it'll suffice. Synchronized instance methods have their monitor associated with the instance Object. Two monitors (the very small toilets) cannot be owned (be synchronized upon) by two different threads at the same time, even when bare need is at hand (beershit or the like).

This means that: when a Thread enters a static synchronized method of a given class, then no other Thread can enter another static synchronized method of that class. But other threads might execute any non-synchronized method, or a synchronized instance method.

There are many tutorials and explanations to be found on the web. Try "java+synchronization+tutorial" in google. And if you want the utmost precise, accurate and reliable picture, check the JLS.
About | Sitemap | Contact