• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized code blocks

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
}
}
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving topic as too difficult for "beginning".
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic