Home Help Java S synchronized Java Keyword

Related Tutorials


 
 

Share on Google+Share on Google+

synchronized Java Keyword

Advertisement
The synchronized is a keyword defined in the java programming language.

synchronized Java Keyword

     

The synchronized is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the synchronized keyword indicates the following :

-- The synchronized keyword may be applied to statement block or to a method.
--  The synchronized keyword provides the protection for the crucial sections that are required only to be executed by a single thread once at a time.
-- The synchronized keyword avoids a critical code from being executed by more than one thread at a time. Its restricts other threads to concurrently access a resource.
-- If the synchronized keyword is applied to a static method, as we will show it with a class having a method SyncStaticMethod through an example below, the entire class get locked while the method under execution and control of a one thread at a time.
-- When the synchronized keyword is applied to an instance method, as we have done with SyncMethod in the example given below, the instance get locked while being accessed and under execution and control of a one thread at a time.
-- When the synchronized keyword is applied to an object, then that object is locked though the code block associated with it get executed by one thread at at time.

Example to use the synchronized keyword within a class in java programming language:

public class Class1{
public synchronized static String SyncStaticMethod(){

}
public synchronized String SyncMethod(){

}

{

public class Class2{
Object Obj;
public String Method2(){
<statements>
synchronized (Obj){
<statements affecting Obj>
}
}
}

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2009

Related Tutorials

Discuss: synchronized Java Keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
sandip
October 19, 2011
error

to check error
DMCA.com