Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized

 
vishnu hiranamayee
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
what is the difference in using synchronized method and synchronized block and also using static with synchronized keyword?
 
mohit junejaa
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Rowland
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic