Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized Blocks  RSS feed

 
vendikonda sravan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SomeOtherClass a= new SomeOtherClass();
void myMethod3(){
synchronized(a){
int i = getBalance(); //statement 1
if (i > 100)
setBalance(i-100); //statement2
}
}

My question is what is the use of giving some third party object to a synchronized block like 'a' in this case? or even passing a string like
void method4 (){
synchronized("x"){
.
.
.
.
}
}

How will it differ from giving 'this' keyword to Synchronized block?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two examples below are equivalent.
The first one is just a little less typing.Jim ... ...
 
vendikonda sravan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know they are equal what if we give some other object "a" replacing "this" wat difference dose it make to the existing block ?

"My question is what is the use of giving some third party object to a synchronized block like 'a' in this case? or even passing a string like"
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vendikonda : The purpose of synchronization is to protect an object from being
changed by any other thread while the current thread is using the it. The protected
(synchronized) object can be any object; second party, third party or whatever.
So I may not fully understand you question. Can you explain further?

Jim ... ...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an external Object (rather than this) to synchronize on provides several things:


1) It allows multiple objects running in different threads to synchronize with each other, via a shared lock instance.

In a real word(ish) situation let's say you have a producer which adds things to a List, and you have a Consumer which reads the List, but also may change it (remove values, for example). It would be unsafe for both the Producer and the Consumer to work on the List at the same time, and so you need to synchronize. Since the Producer and Consumer are not the same Object, they can't synchronize on their self, they need something shared (and in this case, the List is the shared object which needs to be protected, so it makes most sense to synchornize on):




2) The second thing you get from using some object other than this to synchronize on would be granularity. You can have different Objects to lock on for different situations.

Again, in a real world(ish) situation as an example, let's say you have a College Degree Program. You have two different Lists - a List of Teachers, and a List of Students. Each List should be protected from being accessed by multiple threads at the same time, but you don't need to block threads accessing the List<Teacher> one a different thread is accessing List<Student>. So you would want to synchronize on the access to each List using a different lock (and again the List itself makes for a good lock):
 
vendikonda sravan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys the replays helped me understand the topic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!