File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes synchronized blocks for static and non-static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized blocks for static and non-static methods" Watch "synchronized blocks for static and non-static methods" New topic
Author

synchronized blocks for static and non-static methods

adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Hi,

I created two threads and using a single instance of class called the static and non-static methods of that class. Ideally static methods need to be called using the class name and i did that too. Please refer code below.

I synchronized both the static and non-static methods on a private static member of the class whose methods the threads were calling. I noticed that the output was synchronized !!

My questions are:

1)Static methods if synchronized using a synchronized block it usually requires the class instance using the class literal as the monitor (e.g. MyClass.class), then how did it accept a static object !

2)How was the output synchronized as threads calling static methods acquire the class lock and threads calling non-static methods acquire object lock !
Even though i have used synchronized block in both the static and non-static methods based on an object (object is static) ,it really shouldn't synchronize ; or is it special case with synchronized blocks with static objects as monitor ?? Can anyone elaborate ?

3) What is the difference between :



and




Please let me know.

Following is the code i have written:




Thanks,
Adithya.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

In brief: Static fields are per class- So even if you have multiple instances of the class and multiple threads using these instances- They are all synchronized. Its similar to using MyClassName.class.


Mohamed Sanaulla | My Blog
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3956
    
  17

What mohamed said is correct. I am just going to expand a little bit (because I am wordy

adithya narayan wrote:Hi,
1)Static methods if synchronized using a synchronized block it usually requires the class instance using the class literal as the monitor (e.g. MyClass.class), then how did it accept a static object !

Static methods using synchronized blocks do not require using the class literal. They simply use it as default when you declare a synchronized static method:

Uses the SomeTest.class method as the synchronized lock. It would be the same as doing this:

In this case SomeTest.class is an Object, just like any other. The only thing that makes it special is that it is guaranteed to exist in the static context, so it is a convenient 'default' Object to synchronize on - so when you have a synchronized static method the SomeTest.class is what is used. You will find the class literal used in other cases to synchronize with static content solely because it is the 'default' mechanism Java uses, and so is a simple way to make sure everything synchronizes on the same Object (if that is what is required).

2)How was the output synchronized as threads calling static methods acquire the class lock and threads calling non-static methods acquire object lock !
Even though i have used synchronized block in both the static and non-static methods based on an object (object is static) ,it really shouldn't synchronize ; or is it special case with synchronized blocks with static objects as monitor ?? Can anyone elaborate ?

There is no special case. The 'class lock' is simply the lock on the class literal's Object, just like any other Object. It is an Object lock, because SomeTest.class is an Object.

3) What is the difference between :



and




What is the difference? Almost nothing. In the most likely scenario, then the two blocks are synchronized on two different Objects. But, there being no other code shown, there could be absolutely no difference - they could be synchronizing on the same thing. Example:

In that sample, both synchronized blocks will lock on the same exact Object, and behave exactly the same way.

Steve
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Thanks Steve for a neat explanation.
adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Thank you so much for the explanation !!
So, the above sample which i have posted wherein non-static as well as static methods are using a single static object as the monitor for the synchronized block will help in preventing data corruption when multiple threads are manipulating data concurrently using the class's right ?

Also, i have a doubt : what is the meaning of the following sentence ?


The only thing that makes it special is that it is guaranteed to exist in the static context, so it is a convenient 'default' Object to synchronize on



Thanks,
Adithya.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3956
    
  17

Yes, your code will help protect data from being manipulated in multiple threads. It will make sure the data is synchronized between instance methods called from different instances of the same class, as well as from the class' static context.

what is the meaning of the following sentence ?
The only thing that makes it special is that it is guaranteed to exist in the static context, so it is a convenient 'default' Object to synchronize on


Hmm... Not sure if I know what you are asking. It means the class literal is just like any other Object, but it will be created before any static methods or static blocks in that class get executed. since this is a 'known' then it can be counted on for synchronizing at the static class level without additional concerns.
adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Thanks for the clarification !
adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Hi Steve and Mohamed,

I keep coming back to Threads and find something intriguing, hence, i am posting another question on the same topic.

What is the difference between

and


Is it like Object.class will always return a single object which is the class literal's object ?
whereas the former one will return a new object of the Object class everytime i call 'new' on it ?

Can anyone please explain with a simple example how synchronization will be affected if i synchronize static and non-static methods by using
a synchronization block in both the cases.

Also, can someone please put some light on what do you mean by a class literal ? Till now i had assumed it to be a particular classes' object.


Lastly,

Is the following legal ? Because it doesn't work !






Thanks,
Adithya.

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

Does this help (this is a gross simplification), rewrite your code like this ...

Car car = new Car (); // make a new car

Object carManual = Car.class; // give me THE object that describes ALL cars like a car manual

If you synchronize on car you reserve a car for your own use if you sync on carManual you reserve the one and only manual for all cars , the two things are VERY different you can't read a car or drive a manual they are both objects and have completely different locks.

Using synchronized (object) in a static and not static method will give you the same effect IF the object you synchronize on is the same.

It would help if you told us what you think your code should do .






"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

This is becoming interesting !! Actually Chris, i am just experimenting with threads especially with synchronization !! I want to see in what all ways threads can be synchronized.

Previously, i was synchronizing based on a third party static object which i was using as the synchronizing lock. Then i came to know that we can use a class literal as the synchronizing lock !

Both these gave me the same result i expected (synchronized code). Now, after your reply it seems there is a deeper meaning to


Can you please elaborate on the above statement ?

Can you also elaborate on what is the difference between as per the example of the Car and Car Manual you gave:

and


What does it mean by each of the following



and




Can anyone also explain what does it mean by a class literal i.e. a class object ?


Cheers,
Adithya.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

Does reading the java doc for class help your understanding ...

Java class
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronized blocks for static and non-static methods
 
Similar Threads
Need help understanding results from running 2 threads
Need Explanation of unpredicted output even if methods are synchronized
Thread synchronization
locking??
about threads, using locks of the objects in synchronized blocks