userimage
Moe

Java synchronization

Java synchronization always ensures that if a thread executes a synchronized method will get blocked and will be used by the thread one time till thread release the lock. Basically the use of synchronization make sure that:

1: If a thread using a synchronized method no other thread can use the method.
2: If a synchronized method using a resource then no other thread can see the changed value until the thread release the lock.


In general there are 2 type of synchronization:

1: non static synchronization
2: static synchronization


e.g non static synchronization:

public class Count {      
    public void printCount(int n){
        for(int i=1;i<=5;i++){  
            System.out.println(n*i);  
            try{  
                Thread.sleep(400);  
            }catch(Exception e){
                System.out.println(e);}  
            }
        }
    }  
}  
      
public class Thread1 extends Thread {  
    Count t;  
    Thread1(Count t){  
        this.t=t;  
    }  
    
    @Override
    public void run(){  
        t.printCount(5);  
    }  
}  


public class Thread2 implements Runnable{  
    Count t;  
    Thread2(Count t){  
        this.t=t;  
    }
    
    @Override    
    public void run(){  
        t.printCount(100);  
    }  
}  

public class NonStaticSynchronization{  
    public static void main(String args[]){  
        Count obj = new Count();  
        Thread1 t1=new Thread1(obj);  
        Thread2 t2= new Thread(new Thread2(obj));  
        t1.start();  
        t2.start();  
    }  
}  

Output:
5
100
10
200
15
300
20
400
25
500


Synchronization is built around an internal entity known as the lock or monitor. Every object has an lock associated with it. By convention, a thread that needs consistent access to an object"s fields has to acquire the object"s lock before accessing them, and then release the lock when it"s done with them.

And static synchronization to protect your code against this, create a private "lock" object, instance or static, and synchronize on that object instead. At run time every class has an instance of a Class object. That is the object that is locked on by static synchronized methods.

public class Count{  
    synchronized static void printCount(int n){  
        for(int i=1;i<=10;i++){  
            System.out.println(n*i);  
            try{  
                Thread.sleep(4000);  
            }catch(Exception e){
            }  
        }  
    }  
}  

public class StaticSynchronization {  
    public static void main(String[] args) {  
        Thread t1=new Thread(){  
            public void run(){  
                Count.printCount(1);  
            }  
        };  

        Thread t2=new Thread(){  
            public void run(){  
                Count.printCount(10);  
            }  
        };  

        Thread t3=new Thread(){  
            public void run(){  
                Count.printCount(100);  
            }  
        };  

        Thread t4=new Thread(){  
            public void run(){  
                Count.printCount(1000);  
            }  
        };  
        t1.start();  
        t2.start();  
        t3.start();  
        t4.start();  
    }  
}

Output:
1
2
3
4
5
6
7
8
9
10
10       
20
30
40
50
60
70
80
90
100
100
200
300
400
500
600
700
800
900
1000
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000