Question:
Why would you use a synchronized block vs. synchronized method?
Answer:
Synchronized blocks place locks for shorter periods than synchronized methods. Source: CoolInterview.com
I guess, synchronized block or synchronized methods are depends on the user requirements.
Like, if you need a method which should be accessed at thread level then we can declare synchronized method else if some block of code only needs to be synchronized then user can have synchronized block of code.
So, i guess it depends on the requirements that we need. Source: CoolInterview.com
Answered by: Venu | Date: 1/10/2010
| Contact Venu
First of all to achieve Multithreading mechanism in java we should go for synchronization. And this can be done in two ways depending on the requirement.
1. Synchronized block and 2. Synchronized method.
if you go for synchronized block it will lock a specific object.
if you go for synchronized method it will lock all the objects.
in other way Both the synchronized method and block are used to acquires the lock for an object. But the context may vary. Suppose if we want to invoke a critical method which is in a class whose access is not available then synchronized block is used. Otherwise synchronized method can be used.
Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc
For a detail clarification see the below code
for example: //Synchronized block
class A { public void method1() {...} } class B { public static void main(String s[]) { A objecta=new A(); A objectb=new A(); synchronized(objecta){objecta.method1();} objectb.method1(); //not synchronized } }
//synchronized method class A { public synchronized void method1() { ...} } class B { public static void main(String s[]) { A objecta=new A(); A objectb =new A(); objecta.method1(); objectb.method2();
} } Source: CoolInterview.com
Answered by: Poonam Rohilla | Date: 5/5/2010
| Contact Poonam Rohilla
There is not a clear advantage of using synchronized method over block.
Perhaps the only one ( but I wouldn't call it advantage ) is you don't need to include the object refence "this".
Method:
Quote: public synchronized void method() { // blocks "this" from here.... ... ... ... } // to here
Block
Quote: public void method() { synchronized( this ) { // blocks "this" from here .... .... .... .... } /// to here... }
See? no advantage at all.
Blocks do have advantages over methods, most of all flexibility, but, that was not your question. Source: CoolInterview.com
Answered by: marck_don | Date: 6/19/2010
| Contact marck_don
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|