Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
Asif Iqbal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,

can anyone explain me this from K&B

"Synchronized block gives you the ability to have more than one lock for code synchronization within a single object."

thanks to all in advance
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it this way, given some Object, not otherwise defined: If one may use the word synchronized to control the use of that object such that multiple parts of a program would not interfere, it would have to be that several things could be going on at once. This is somewhat challenging to explain in that operating systems that can do more than one thing at a time exists. Also, systems that can only do one thing at a time.

How to explain synchronization then? Like they did.

Try thinking of what they are saying as though multiple computers could use the object at the same time. Changing something while another is using something does not work.
 
Asif Iqbal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nicholas!

thanks for reply,but still......
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll take a try at it.

Before talking about synchronized block let me "set the stage" by looking at synchronized method.

Every object (from Object) has a single lock by default.

When you use synchronized methods within a class, all of the synchronized methods in that class use the object instance's lock as the lock for ALL of them. So, if you call methodA() and it is synchronized and another thread later calls methodB() (also synchronized, AND... assume that the first thread is STILL in methodA()) then methodB() will be held off waiting on the lock from the object instance. Be sure that you have that concept.... each object instantiated from a class has it's OWN lock, separate from the other object instances. Locks will NOT synchronize entry between two different instance objects.

Just one more twist before going to synchronized blocks!

When your synchronized method is STATIC the CLASS itself is the lock. Any threads calling a static synchronized method will be synchronized by the class itself not a specific object.

Now, synchronized blocks are set apart because they are the only method of synchronization that allows the programmer to specify what object to use as the lock-object. So, unlike a synchronized method, the programmer can declare an object (even just an instance of Object) and specify that as the lock object so that, now, synchronization can be performed on separate distinct locks.

Just think about this a bit and see if you become comfortable with it.
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I did get the concept & i am trying to validate my understanding here.
Suppose this code is written inside class Sue

foo being and object of class Foo.

Further assume that Foo itself has a synchronized method.

So according to my understanding then if a thread was executing within a synchronized method of Foo then no other thread would be able to enter inside
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Asif Iqbal:
hello all,

"Synchronized block gives you the ability to have more than one lock for code synchronization within a single object."


does this means this


or this

or none of these......
[ August 18, 2008: Message edited by: Ankit Garg ]
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That gives you the
ability to have more than one lock for code synchronization within a single object.


As you enter a sync method of a class , the thread have the lock of the object i.e (the instance on which method is called) as you write Synchronized word with the method as below


But in case of Sync blocks we have the ability to have lock of other objects too withinn a single object. I tried to give it as below code-:


i guess they meant to say this, Thanks.
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I guess mine & pranav's understanding is fairly the same ... However what i was asking for is a situation where we would need to use a synchronized block locked on another object & the effects of the same.

With reference to my above post, specifically the effect on the object used as the lock

Further assume that Foo itself has a synchronized method.

So according to my understanding then if a thread was executing within a synchronized method of Foo then no other thread would be able to enter inside



Could anyone please confirm this understanding ?
[ August 18, 2008: Message edited by: sid sree ]
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can get an implementation of same in K&B's book. There is a prog for calculation- Calculator. It gives some idea about it. There the thread in its run method and gets synchronized on a different object C till it finishes the addition and then gets notified to release the lock on that object C. Check it, it is helpful
 
Steve Ng
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that: synchronized is used at method signature or a block of code within a method:

+ when you mark a method as synchronized, you are saying that this method is to be accessed by only one thread at a time. Another thread is able to access it only after the current thread exists the method.

+ When you use synchronized block:

synchronized(object){
//More code here
}

You are saying: The thread which is running this block in this method has the lock on a particular object(Note that 1 object has 1 lock). So no other thread may access that object at the same time..

Methods wait() and notify() must be called within a synchronized block because the thread is supposed to release the lock on the object for others threads to use. If the thread does not own the lock on that object, IllegalMonitorStateException occurs

This is my understanding. Please correct me if i'm wrong.

[ August 18, 2008: Message edited by: Steve Ng ]
[ August 18, 2008: Message edited by: Steve Ng ]
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot guys .... i guess i have gotten the hang of it now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic