Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 20 November 2016

What is Thread Synchronization In Java ?

Previously we have discussed about
Thread Synchronization

Threads communicate by sharing fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.

Synchronization is a technique through which we ensure that resource will be used by only one thread at a time.The synchronization keyword in java creates a block of code referred to as critical section. Every java object with a critical section of code gets a lock associated with the object. To enter in critical section a thread need to obtain the corresponding objects lock.

Synchronized  Suntax:
synchronized (object){
// code here is synchronized 
}

Why we use synchronization ?
When two or more threads access a shared resource at a same time, then it will result in deadlock.
For Example : Suppose we have two threads T1 and T2 , T1 starts execution and save certain values in file abc.txt, and T1 return some result on the basic of those values.Meanwhile T2 starts execution before T1 returns result, and T2 changes the value stored in file abc.txt .Now obviously T1 return wrong result.
To prevent such Problems Synchronization was introduced.

Synchronized Methods:
The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements. The more complex of the two is synchronized statements. To make a method synchronized, simply add the synchronized keyword to its declaration:

public class SynchronizationExample {
private int c = 0;
public synchronized void increment(){
c++;
}
public synchronized void decrement(){
c--;
}
public synchronized int value(){
return c;
}
}

Note : that constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

Example Without Synchronization :
In this example,we have not use synchronization(or Synchronized  keyword ) so, we will get inconsistent output .

class thread_A extends Thread{
SynchronizationExample t;
public thread_A(SynchronizationExample t) {
this.t = t;
}
public void run(){
t.printTable(10);
}
}
class thread_B extends Thread{
SynchronizationExample t;
public thread_B(SynchronizationExample t) {
this.t = t;
}
public void run(){
t.printTable(200);
}
}
public class SynchronizationExample {
void printTable(int num){
for(int i=1;i<=5;i++){
System.out.println(num*i);
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizationExample obj = new SynchronizationExample();
thread_A t1 = new thread_A(obj);
thread_B t2 = new thread_B(obj);
t1.start();
t2.start();
}
}
Program Output:
10
200
20
400
600
30
800
40
1000
50

Example With Synchronization :
In this example,we have use synchronization(or Synchronized  keyword ) so, we will get consistent output .

class thread_A extends Thread{
SynchronizationExample t;
public thread_A(SynchronizationExample t) {
this.t = t;
}
public void run(){
t.printTable(10);
}
}
class thread_B extends Thread{
SynchronizationExample t;
public thread_B(SynchronizationExample t) {
this.t = t;
}
public void run(){
t.printTable(200);
}
}
public class SynchronizationExample {
synchronized void printTable(int num){ //synchronized method  
for(int i=1;i<=5;i++){
System.out.println(num*i);
try {
Thread.sleep(200);
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizationExample obj = new SynchronizationExample();
thread_A t1 = new thread_A(obj);
thread_B t2 = new thread_B(obj);
t1.start();
t2.start();
}
}
Program Output:
10
20
30
40
50
200
400
600
800
1000

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment