wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Synchronized code blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized code blocks" Watch "Synchronized code blocks" New topic
Author

Synchronized code blocks

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252


This question is regarding the synchronized blocks. Unlike a synchronized method, that always locks this object, synchronized block can be used for this object or a third party object. But... if the lock is placed on another object, the statements will not be synchronized.

So, what is the point of having that option?


In other words, take a simple synchronized method MyMethod1 as follows:-

synchronized void myMethod1(){
int i = getBalance(); //statement 1
if (i > 100)
setBalance(i-100); //statement2
}


This can be replaced safely with the following MyMethod2.

void myMethod2(){
synchronized(this){
int i = getBalance(); //statement 1
if (i > 100)
setBalance(i-100); //statement2
}
}


But, if we try to replace it with the following MyMethod3, statement1 & statement2 will not work atomically, so why does Sun even offer that option?

synchronized void myMethod3(){
synchronized(new SomeOtherClass()){
int i = getBalance(); //statement 1
if (i > 100)
setBalance(i-100); //statement2
}
}


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Hi

Look at your code carefully

You are creating new object everytime for every thread.

So each and every thread gets the lock on different object not on the same object.

Try declaring the object outside the synchronized block and exectue it.



SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Hi Siva,

Thanks for taking the time to answer my question. I now realize that I made a typo. MyMethod3 is *not* synchronized. Only the code block containing statements 1 & 2 should be synchronized in MyMethod3.

In that case, when I run the code as you posted (create an instance of SomeOtherClass outside the synchronized block and then run it), the whole block is not synchronized - at least that is the result that I got. Here is the listing:-


Wondering what I'm missing here.

- Nidhi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Nidhi Sar wrote:
In that case, when I run the code as you posted (create an instance of SomeOtherClass outside the synchronized block and then run it), the whole block is not synchronized - at least that is the result that I got. Here is the listing:-


The purpose of the synchronized blocks is to give you full control of the scope. The standard synchronized method, will use the "this" instance, which means that method calls to the same instance will block on each other. This is, of course, may not be what you want.

(1) What if you want the synchronization to be based on the data that you are working on? (2) What if you want many different instances (and of different class types) to wait on each other? (3) What if you want to ensure that you lock out certain things from happening before you do your task? With the synchronize blocks, you can do that.

And BTW, your example is useless. You created a local object, which you don't expose to other threads, and synch on it. Since, no other thread can synch on that same object -- it serves no purpose.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Again you delcared that variable as local and each thread has its own copy of local variable.

Declare the variable as instance variable and you will get the desired result.

Henry has given better explaination also.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Moving topic as too difficult for "beginning".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
And welcome to JavaRanch
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Got the first part!

I tried the same thing with an instance variable. All operations performed inside the block get synchronized... one thread at a time.

Thanks Henry, Siva for helping me on this.

The second part is still a mystery. Why is synchronizing on this different from synchronizing on some_other_object? After all, as I understand it, synchronizing locks up an object and the next thread has to wait at that point till the first thread frees up the object. So the following two code snippets should have the same effect:




In either case, the next thread that comes along has to wait for the object to free up before the code is performed. So the effect should be the same, //some_code would get synchronized. Whether I use object a or object b, would be irrelevant.

I'm sure there is a fatal flaw in the logic I just proposed, but I cannot figure out what.

- Nidhi
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
lets say you want to protect only particular data from an object not all the data present in the object then you can use the synchronized block for that particular data object.

e.g. in the below code lets say you want to protect only a1 from multiple access then instead of getting lock on "this" object which also try to protect the data a2 which you do not need in this case.

You simply get the lock on a1 leaving a2 which can be accessed by any number thread at the same time.

Hope this cleared your doubt.


Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Yes, Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Synchronized code blocks
 
Similar Threads
Assert statements
class methods vs instance method
Synchronized Blocks
synchronized method
Using Reflection to call super class method