Sie sind auf Seite 1von 4

7/14/13

synchronized (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
vishnu hiranamayee Greenhorn Joined: Feb 23, 2006 Posts: 17

synchronized
posted 3/14/2006 12:30 PM

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

posted 3/14/2006 3:04 PM

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

posted 3/15/2006 8:27 AM

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 {

https://www.coderanch.com/t/253271/java-programmer-SCJP/certification/synchronized

1/4

7/14/13

synchronized (SCJP forum at JavaRanch)

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

posted 3/15/2006 3:15 PM

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

https://www.coderanch.com/t/253271/java-programmer-SCJP/certification/synchronized

2/4

7/14/13

synchronized (SCJP forum at JavaRanch)

Graham

Aleksander Zielinski Ranch Hand Joined: Nov 11, 2005 Posts: 127

posted 3/15/2006 4:25 PM

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

posted 3/15/2006 5:01 PM

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

posted 3/15/2006 6:18 PM

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

posted 3/15/2006 7:05 PM

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

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

https://www.coderanch.com/t/253271/java-programmer-SCJP/certification/synchronized

3/4

7/14/13

synchronized (SCJP forum at JavaRanch)

subject: synchronized

Similar Threads Synchronized Block and Synchronized method Synchroniztion Question synchronized synchronized syncronization
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 09:00:54 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/253271/java-programmer-SCJP/certification/synchronized

4/4

Das könnte Ihnen auch gefallen