CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: What synchronization constructs does Java provide? How do they work?

Answer:



Category Java Interview Questions & Answers - Exam Mode / Learning Mode
Rating (3.5) By 17 users
Added on 10/13/2009
Views 1514
Rate it!
Question: What synchronization constructs does Java provide? How do they work?
Answer:

Source: CoolInterview.com


When you synchronize on an object your thread gains exclusive use of that object until synchronization ends or wait is called on the object.

Language support for synchronization:
1. Use the synchronized block:
In a method you may:
synchronized(object)
{
code here
}

synchronized(SomeClass.class)
{
code here
}

In the first case access to the instance variables/methods of the class are synchronized. In the second case access to the static methods/fields are synchronized.

2. Declare the method synchronized:
public synchronized foo()
{
code here
}

public static synchronized foo()
{
code here
}

These two act just like synchronized(this) or synchronized(MyClass.class) (depending if the method is static or not).

In addition the Object class defines the wait, notify, notifyAll, and sleep methods. To execute any of these methods the object must be in a synchronized block (this can be in a calling method). The compiler will not check this, a runtime error will be thrown if this is not the case. The wait method temporaly releases the lock on the object. The notify/notifyAll method allows a previously waiting thread to continue to execute.

Also when executing the static block of a class:
public class Foo
{
static
{
code here
}
}

A special lock is placed on the class, this lock is different from the synchronized lock, as no other thread may access the class until the static block is done.
Source: CoolInterview.com

Answered by: Susanta Panigrahi | Date: | Contact Susanta Panigrahi Contact Susanta Panigrahi


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.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
Are constructors inherited?Can a subclass call the parent's class constructor?When?
View Answer
Does Java have destructors?
View Answer
What does the "abstract" keyword mean in front of a method? A class?
View Answer
Name four methods every Java class will have.
View Answer
Given a text file, input.txt, provide the statement required
View Answer
Discuss the differences between creating a new class, extending a class and implementing an interface; and when each would be appropriate.
View Answer
What's the difference between the == operator and the equals() method? What test does Object.equals() use, and why?
View Answer
Why do you create interfaces, and when MUST you use one?
View Answer
What is the difference between instanceof and isInstance?
View Answer
Are there any other 'marker' interfaces?
View Answer
How many methods do u implement if implement the Serializable Interface?
View Answer
What are the advantages of OOPL?
View Answer
What do mean by polymorphism, inheritance, encapsulation?
View Answer
What do you mean by static methods?
View Answer
What do you mean by virtual methods?
View Answer
Given two tables Student(SID, Name, Course) and Level(SID, level) write the SQL statement to get the name and SID of the student who are taking course = 3
and at freshman level.
View Answer
What are the disadvantages of using threads?
View Answer
Write the Java code to declare any constant (say gravitational constant) and to get its value.
View Answer
What do you mean by multiple inheritance in C++ ?
View Answer
Can you write Java code for declaration of multiple inheritance in Java ?
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 Java Interview Questions & Answers - Exam Mode / Learning Mode




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
Cache = 0.03125 Seconds