File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Thread synchronization inside static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread synchronization inside static method" Watch "Thread synchronization inside static method" New topic
Author

Thread synchronization inside static method

Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Hi All,
I've a had an issue with Thread synchronization inside static method, whereas I'm not allowed to use "wait" method of Object type inside this function and signature synchronized keyword is needed. Since wait belongs to Object class, inside a static method couldn't referred.

When I made the static method to instance method of that class, it works well. (for some reason my most of the methods are static since its helper class). Also without synchronized keyword of the function run time error throws for wait() irrespective of class method. Is there any way of using synchronization with in static method? Your help is much more helpful to me. Thanks in advance.


No pain, No gain.
OCJP 1.6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

The wait method is a non-static method in class Object. You cannot call it from a static method by just invoking wait(); because static objects are not called on objects - so there is no current object to invoke the wait on. Ofcourse, you can call wait on another object from inside a static method:

You do have to synchronize on the object, otherwise you'll get an IllegalMonitorStateException.

The key thing to understand is that synchronization happens on objects in Java. Each Java object has a lock associated with it. When you synchronize on an object, then inside the synchronized block the thread that's running it holds the lock. Only one thread at a time can hold the lock of an object. Other threads that want to run the synchronized block are forced to wait until the current thread running it releases the lock.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kumaravadivel Subramani wrote:Hi All,
I've a had an issue with Thread synchronization inside static method, whereas I'm not allowed to use "wait" method of Object type inside this function and signature synchronized keyword is needed. Since wait belongs to Object class, inside a static method couldn't referred.


You can synchronize inside a static method just as you can in any other method (or constructor, or initializer block), and you can call wait() on your lock object just like you would anywhere else. The only thing you can't do is synchronized (this), since there is no "this", and, accordingly you can't do this.wait(), or the unqualified wait(), which is the same thing. But that has nothing to do with syncing or the wait() method or synchronization. That's just a result of the fact that there's no "this" reference, so you can't do stuff that uses "this" explicitly or implicitly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
If there is no this, don’t you synchronise on the Class<?> object?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:If there is no this, don’t you synchronise on the Class<?> object?


Yes and no.

Whether there's a this or not, you can synchronize on any object you can reach. It's just that declaring a method as synchronized, as opposed to using a sync block that names an explicit lock, is (mostly) just syntactic sugar for syncing the entire body of the method on this or on the class object.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kumaravadivel Subramani wrote:
When I made the static method to instance method of that class, it works well. (for some reason my most of the methods are static since its helper class). Also without synchronized keyword of the function run time error throws for wait() irrespective of class method. Is there any way of using synchronization with in static method? Your help is much more helpful to me. Thanks in advance.


Okay, having read Campbell's response and then coming back and reading your question more closely, I think I understand now.



Does that clear it up?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Thank you. So a static synchronized method would implicitly equate to static synchronized(Foo.class).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Thank you. So a static synchronized method would implicitly equate to static synchronized(Foo.class).


Well, yeah, but it would equate to legal syntax. :-)

See my follow-up post above.
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Thanks you all folks.. Really your information have cleared my doubts. Just for your cool heads..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread synchronization inside static method
 
Similar Threads
How to block access based on elements existance?
threads
Depth of Garbage Collection, Thread synchronization
wait()
A static synchronized method and a non-static synchronized method will not block each other, ever.