Sponsored Links

Interview Questions



INTERVIEW QUESTIONS J2EE CORE JAVA DETAILS

Question: Why would you use a synchronized block vs. synchronized method?

Answer: Synchronized blocks place locks for shorter periods than synchronized methods.

Category Core Java Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.4) By 6622 users
Added on 8/24/2014
Views 68012
Rate it!

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 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 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 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.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification Enter the above shown code: *
Inform me about updated answers to this question

Related Questions
View Answer
What are the similarities between an array and an Array List?
View Answer
how many ways to create Thread and which one is good? runnable interface or Thread class?
View Answer
What do u mean contructor in java ?
View Answer
What do u mean by "method" in java ?
View Answer
what is main purpose of interface?
View Answer
what is main purpose of abstract class?
View Answer
What is the difference between throw and throws?
View Answer
what is the class variables ?
View Answer
Describe the wrapper classes in Java.
View Answer
What are Access Specifiers available in Java?
View Answer
Explain the different forms of Polymorphism.
View Answer
Explain the Polymorphism principle.
View Answer
Explain the Inheritance principle.
View Answer
Explain the Encapsulation principle.
View Answer
Describe the principles of OOPS.
View Answer
Explain garbage collection?
View Answer
Explain the user defined Exceptions?
View Answer
How to define an Interface?
View Answer
How to define an Abstract class?
View Answer
What is difference between an Abstract class and Interface?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Core Java Interview Questions & Answers - Exam Mode / Learning Mode



User Options
India News Network

Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal



Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




Copyright ©2003-2024 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions