Thread Synchronization

The mechanism to accomplish the mutual access of shared resource for two or more thread is referred as the synchronization .when two or more thread access single resource then it is ensured that only one thread should access it at a time.


The core concept of synchronization is the monitor. Only one thread can get the access to monitor at a time or we can say only one thread can enter in the monitor at a time. If one thread using the shared resource than it is said to be in the monitor and it acquires the lock on the monitor so no anther thread has the permission to access the shared recourse. When first thread finishes its execution then it release the lock from the monitor and frees the monitor to use by another thread.

In the java all the object have their implicit monitor. So it is easy to synchronize any object or any method of object.
There are two ways in which execution of code can be synchronized:
·         synchronized methods
·         synchronized blocks

synchronized methods

Method that should be accessed by one thread at a time is synchronized by the placing synchronized key word as the access modifier before the method. The thread then access it after acquire the lock for it.
When any thread calls the synchronized method, that thread enters in the monitor and all other threads that try to call same synchronized method at the same instance have to wait. When the thread that entered in the monitor (or that accessing the synchronized resource) returns from the synchronized method, the thread waiting, acquires the monitor. So one time only one thread uses the synchronized method.
Bellow in the given example there is a class Shared which has the method print() to print < hi hello> on the console output. There are two separate thread classes. when the object of these are classes are created the object of the class Shared has to passed through the constructor and the reference of that object is stored in to the s reference variable. Both thread classes implements run method and in the run methods they call the print method of the object of reference in s.

Now the main class creates the object of the Shared class ant the reference is stored in the reference variable obj. after it, objects of thread classes are created and the obj is passed to the constructors of the thread classes. So the reference of the same object is passed for both thread’s objects. Now the reference variable s of each thread object refers the same object. And they are calling the print method of the same object in the run method. At last we can say they are calling same print method. Now at this movement it is our responsibility that we ensure that first thread class the print method then second thread calls it

class MyThread1 extends Thread
{    
    Shared s;
    MyThread1(Shared s)
    {
        this.s=s;
    }    
    public void run()
    {    
        s.print();
    }
}
class MyThread2 extends Thread
{    
    Shared s;
    MyThread2(Shared s)
    {
        this.s=s;
    }    

    public void run()
    {    
        s.print();
    }
}

class sync
{
    public static void main(String ar[])
    {
        Shared obj=new Shared();
        MyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start();
        t2.start(); 
    }
}

class Share
{
    public synchronized void print()
    {
         System.out.print("< Hi ");
         try{Thread.sleep(1000);}catch(InterruptedException e){}
         System.out.print(" Hello >\n");
    }
}
OUTPUT
< Hi  Hello >
< Hi  Hello >

No comments:

Popular Posts