Sunday, December 30, 2012

Synchronization Concept of Multithreading in Java

What is Synchronization ?

Whenever we work with multithreading concept of Java we generally come across getting inconsistent data problem.

First We understand Why this Problem occurs then we move on to the solution....

So just assume we have multiple thread(let's take t1,t2 and t3) in an application that we are executing.
Thread t1 is on process i.e it is executing logic of java program.
at the same time another thread t2 enters in to the work area for executing the same logic on which t1 is on .
At this point of time both t1 and t2 thread are executing same logic so there might be chance to generate unexpected output due to clash...
So this is the kind of problem that we generally get while working with multiple threads in the same program.
when multiple threads are collectively working on common/sharable resourse then those multiple threads generate inconsistent result .

To avoid this problem inconsistent result we must use the concept of Synchronization.

we can also define like..

The process of allowing 1 thread among multiple thread into the area which is Sharable to perform read & write operation in known as Synchronization.




How to Come up with this Issue..
*************************************
To resolve this issue Synchronization concept is recommended..


Thread Synchronization Technique.
*****************************************
 Thread Synchronization Techniques are of 2 type.


Synchronized Method
***************************

Any method followed by
synchronized keyword will act as a Synchronized method.
And it will allow only 1 thread at a time to access the sharable resource.

Based on writing
synchronized keyword before the method definition.
methods are classified into 2 type.

1) Synchronized instance method:

     ********************************* 

Placing synchronized keyword before any ordinary instance method definition.
Syntax:
******
synchronized <Return Type> <method_name> (list of formal param if any)
{

-------------------------
-------------------------
Block of Statement
-------------------------
-------------------------

}  
Example
******* 
Class Account
{

private int bal=0;

synchronized void deposit(int amt)
{
bal=bal+amt;
System.out.println("Current bal="+bal);
}

}

2) Synchronized static method:
     ****************************** 

Placing Synchronized keyword before any static method definition.


Syntax:
******
synchronized static <Return Type> <method_name> (list of formal param if any)
{

-------------------------
-------------------------
Block of Statement
-------------------------
-------------------------

}  
Example
*******
Class Account
{

private int bal=0;
synchronized static void deposit(int amt)
{
bal=bal+amt;
System.out.println("Current bal="+bal);

}

}

 As long as one thread is executing the above Synchronized static deposit(-), then the JVM locks the corresponding class called Account.

Synchronized block

**********************************
Synchronized block is an alternative technique of Synchronized  method.

Synchronized block must be always written inside the normal instance method/inherited non-Synchronized instance method but not in static method because it is always recommended to override instance method but not static method..

Since we are writing Synchronized block in non Synchronized instance method. so this instance method
always locks object of current class.



Syntax:
******
synchronized (Object of current class)
{

-------------------------
-------------------------
Block of Statement
-------------------------
-------------------------

}

Example******* 
//Account.java 
package app;
public interface Account
{
void deposit(int);
}
//SAccount.java
public class SAccount implements app.Account
{
 public void deposit(int amt)
 {
  synchronized(this)
  {
   bal=bal+amt;
   Sysytem.out.println("Current Bal="+bal);
  }
 }
}

No comments:

Post a Comment