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

synchronized block

Asif Iqbal
Greenhorn

Joined: May 02, 2008
Posts: 10
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

Joined: Sep 17, 2006
Posts: 1282
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.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Asif Iqbal
Greenhorn

Joined: May 02, 2008
Posts: 10
hi Nicholas!

thanks for reply,but still......
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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.


------------------------
Bob
SCJP - 86% - June 11, 2009
Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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 ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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

Joined: Jun 28, 2007
Posts: 188

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

Joined: Mar 20, 2006
Posts: 284
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

Joined: Aug 10, 2008
Posts: 17
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

Joined: Jun 28, 2007
Posts: 188

Thanks alot guys .... i guess i have gotten the hang of it now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized block