permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized 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" Watch "synchronized" New topic
Author

synchronized

vishnu hiranamayee
Greenhorn

Joined: Feb 23, 2006
Posts: 17
Hi
what is the difference in using synchronized method and synchronized block and also using static with synchronized keyword?
mohit junejaa
Ranch Hand

Joined: Feb 24, 2006
Posts: 41
in synchorinized block u r reducing the overhead by putting only the code that shd be thread safe is the block unlike a synchronized method in which u make the entire method thread safe
in case of static u need to acquire lock of entire class . that means the whole class becomes thread safe


scjp 1.4
David Rowland
Greenhorn

Joined: Jan 17, 2006
Posts: 5
If you synchronize a block, you must choose an object to synchronize on, and the object you choose doesn't have to be the object containing the method your block is in. For example:

public class MyObject {

String stringLock = "None shall pass!!";

public void methodA() {

synchronized(stringLock) {
//threadsafe code here
}
}
}

If you used sychronized with methodA(), then the *instance* of MyObject would automatically provide the lock.

For static methods the lock comes _not_ from the instance of MyObject, but from the MyObject Class that was loaded into memory when the program started.
Graham Walsh
Greenhorn

Joined: Mar 08, 2006
Posts: 23
Hi,

>For static methods the lock comes _not_ from the instance of MyObject, but >from the MyObject Class that was loaded into memory when the program >started.

say I have something like this;

class MyObject
{
public static synchronized void ASynchronizedMethod()
{
}

public static synchronized void anotherSynchronizedStaticMethod()
{
}
}

are you saying that a call to ASynchronizedMethod causes a lock to be acquired from the class (not instance... as it's static). If thats the case then is it the case that a call to anotherSynchronizedStaticMethod at the same time will block until the lock from the first ASynchronizedMethod call (acquired from class not instance) has completed.

I guess what I'm trying to fathom is this. For static methods, locks are acquired from the class and thus only one static synchronized method can be invoked at any one time as locks are acquired from the class and not instance.

Is that incorrect? Sorry if it's gibberish!

thanks

Graham
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Yes, you are right, but keep in mind that if at the same time you are going to access static fields through instance methods (even if they are synchronized) you'll be able to do so and modify the static field, so keep your static methods access static fields and instance methods access instance members.
Graham Walsh
Greenhorn

Joined: Mar 08, 2006
Posts: 23
thanks for the reply, I'm not quite sure I fully understand though. Could you elaborate? (please ? )

have a nice day.

G
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
That's probably because of my English

I meant, when it comes to using synchronized static and non-static methods, make sure then that your instance methods modify instance fields only. Even if you have synchronized static method that modifies some static field, you can modify that static field using non-static method even if that non-static method is synchronized too.
Graham Walsh
Greenhorn

Joined: Mar 08, 2006
Posts: 23
there's nothing wrong with your english. Thanks for the reply.

Read your last reply again... it's a MOUTHFUL!

cheers man, have a nice day.

G
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: synchronized