Beruflich Dokumente
Kultur Dokumente
Java FAQ
Recent Topics
Register / Login
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?
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
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
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.
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
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.
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.
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
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 .
https://www.coderanch.com/t/253271/java-programmer-SCJP/certification/synchronized
4/4