Social Icons

twitterfacebookgoogle pluslinkedinrss feed

Wednesday, January 06, 2010

Thread Synchronization

When multiple threads are accessing a common method through their run methods, the output of that particular program is unpredictable, because while one thread is gone half way through the common method, another thread would gain the common method.

Similarly the common method execution result of multiple threads can have a variety and also may attack to the accuracy of the expected result.

If you need to let the common method accessible by one thread at one time you have to done it through synchronization.

Imagine you expect to increase the value of number in Thread A, and the value of number Thread B by one via calling the common method count().

Examine the following code and see the result by executing it.

class SynchDemo{
    int common=7;
    public static void main(String args[]){

        SynchDemo demo=new SynchDemo();

        A a =new A();
        a.active(demo);
        a.start();

        B b=new B();
        b.active(demo);
        b.start();

    }

    public int count(int x){
        common=x;
        try{
            Thread.sleep(1000);
        }catch(Exception ee){
        }
        return ++common;
    }
}

class A extends Thread{
    int number=5;
    SynchDemo demo;

    public void run(){
        System.out.println("Start Thread A\n");
        System.out.println("A before increase:"+number+"\n");
        System.out.println("A after increase by one:"+demo.count(number)+"\n");
    }

    public void active(SynchDemo demo){
        this.demo=demo;

    }

}

class B extends Thread{
    int number=10;
    SynchDemo demo;

    public void run(){
        System.out.println("Start Thread B\n");
        System.out.println("B before increase:"+number+"\n");
        System.out.println("B after increase by one:"+demo.count(number)+"\n");
    }

    public void active(SynchDemo demo){
        this.demo=demo;

    }

} 

The result can be,
The result of the program is wired, try multiple times.

Increased value of Thread A or Thread B becomes incorrect most of the time.

That's because another thread enters to the method and the value will be updated by that thread. So the previous thread gets the updated value to increase. Let's use Synchronization....

In Synchronization we can make a code mutually exclusive to multiple threads through a common object. When we add the key word 'synchronized' before the common method, the Thread which calls the method will put a lock to the object it uses to call the method.

Examine the following code and see the result.

class SynchDemo{
   
    int common=7;
   
    public static void main(String args[]){
       
        SynchDemo demo=new SynchDemo();
       
        A a =new A();
        a.active(demo);
        a.start();

        B b=new B();
        b.active(demo);
        b.start();

    }

    public synchronized int count(int x){
        common=x;
        try{
            Thread.sleep(1000);
        }catch(Exception ee){
        }
        return ++common;
    }
 }

class A extends Thread{
    int number=5;
    SynchDemo demo;

    public void run(){
        System.out.println("Start Thread A\n");
        System.out.println("A before increase:"+number+"\n");
        System.out.println("A after increase by one:"+demo.count(number)+"\n");
    }

    public void active(SynchDemo demo){
        this.demo=demo;

    }

}

class B extends Thread{
    int number=10;
    SynchDemo demo;

    public void run(){
        System.out.println("Start Thread B\n");
        System.out.println("B before increase:"+number+"\n");
        System.out.println("B after increase by one:"+demo.count(number)+"\n");
    }

    public void active(SynchDemo demo){
        this.demo=demo;

    }

}


The result is guranteed to increase the valuse of threads by one.



Here the common method is count(). The object used to call the method is ‘demo’. So the first thread which calls the method count(), gain the lock of the object 'demo'.

As next thread also need 'demo' object to call method count() it has to wait until First thread come out of the synchronized method and release the lock. Soon The next thread can gain the lock in 'demo' and execute count().

The most important thing is,
Both methods have used the same object to call the common method. Otherwise synchronizing the method won't help. Try using different objects.

Article Prepared by : +Lakmali Baminiwatta  Blog
Post a Comment