CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS J2EE JAVA DETAILS
Question :
What synchronization constructs does Java provide? How do they work?
Category Java Interview Questions
Rating (5.0) By 1 users
Added on 10/13/2004
Views 1393
Rate it!
Answers:



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.



 Posted by: Susanta Panigrahi    

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.
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
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

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL Java Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/386/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.84