by Dinesh Thakur

When more than one thread has to use a shared resource, Java finds a way of ensuring that only one thread uses the resources at one point of time; this is called synchronization.

In Java, each object is associated with a lock. The term lock refers to the access granted to a particular thread that has entered a synchronized method. Monitor refers to a portion of the code in a program. It contains one specific region (related to data or some resource) that can be occupied by only one thread at a time. This specific region within a monitor is known as critical section. A thread has exclusive lock from the time it enters the critical section to the time it leaves. That is, the data (or resource) is exclusively served for the thread, and any other thread has to wait to access that data (or resource). Some common terminology while using monitors is as follows: entering the critical section is known as acquiring the monitor, working with the critical section (data or resource) is known as owning the monitor and leaving the critical section is known as releasing the monitor.

The key concept in synchronization is the monitor. The monitor holds exclusive lock that

can be owned by only one object at a time. The monitor allows only one thread to execute a synchronized method on the object at one time. This is accomplished by giving the ownership of the monitor to that object, or in other words, locking the object when the synchronized method is invoked. An object thus locked is also denoted to have obtained the lock.

When there are several synchronized methods attempting to act on an object, only one synchronized method may be active on an object at one instant of time and all other threads attempting to invoke synchronized methods must wait. Thus, other threads that attempt to lock the already locked monitor will be suspended. When the execution of that synchronized method is complete, the lock on the object is opened and the monitor allows the highest-priority runnable thread attempting to invoke a synchronized method to proceed.

A thread executing in a synchronized method may determine that it cannot proceed, so it

voluntarily calls wait (). This removes the thread from contention for the processor and from contention for the monitor object. The thread now remains in the waiting state while other threads try to enter the monitor object. When the thread executing the synchronized method completes, it can notify a waiting thread to become ready again so that now that thread can attempt to obtain the lock on the monitor object again and be executed. The notify () method acts as a signal to the waiting thread that the condition to be satisfied for running the waiting thread is satisfied now. Then, the waiting thread can enter into the monitor. If a thread calls notifyAll (), then all threads waiting for the object become eligible to reenter the monitor (that is, they are all placed in a runnable state). Only one of those threads can obtain the lock on the object at a time.

The methods wait (), notify () and notifyAll () are the methods of the class Object. Since every     user class inherits the class Object, these methods are available to all classes. Therefore, any object can enter into a monitor.

The monitor maintains a list of all threads waiting to enter the monitor object to execute

synchronized methods. A thread is inserted in the list and waits for the object if that thread calls a synchronized method of the object while another thread is already executing is a synchronized method of that object. A thread is inserted in the list also if the thread calls the wait () method while operating inside the object. Threads executing synchronized methods explicitly call wait () inside the monitor upon completion of a synchronized method. Other threads that are blocked because the monitor was busy can now proceed to enter into the object. Threads that explicitly invoked wait () can only proceed when they are notified by the notify or notifyAll methods called by another thread. When it is acceptable for a waiting thread to proceed, the scheduler selects the thread with the highest priority.

           

The keyword synchronized is used to synchronize the threads and there are two ways to do it: by using synchronized methods and using a synchronized statement.

Synchronized methods

If two or more threads modify an object, the methods that carry the modifications are declared synchronized. Constructors need not be synchronized because a new object is created in only one thread. The general syntax for declaring a method synchronized is the following:

Modifier synchronized <return_type> <method_name> (parameters)

To get a clear understanding of how this works, consider the Program.

Program Using unsynchronized threads.

import java.io.*;

class Class_A

{

    void printValue ()

     {

           try

          {

               for (int i =0; i<=5; i++)

               {

                     System.out.print ( i +" ");

                     Thread.sleep (1000);

                 }

            }

            catch (InterruptedException)

            {

             }

     }

}

class ThreadExample implements Runnable

{

    Class_A ob1;

    Thread t;

    ThreadExample (Class_A c)

    {

       this.ob1 =c;

       t = new Thread (this);

    }

    public void run ()

   {

       ob1.printVa1ueO;

    }

public static void main (String [] args)

{

    Class_A ca = new Class_A ( );

    ThreadExample one = new ThreadExample (ca);

    one.t.start ();

    ThreadExample two = new ThreadExample (ca);

     two.t.start ();

    ThreadExample three = new ThreadExample (ca);

     three.t.start ();

}

}

If we run the above example, we expect the following output:

0 1 2 3 4 5 0 1 2 3 4 5

0 1 2 3 4 5

But the result is unpredictable; it may result in the following:

0 0 0 1 1 1 2 2 2 3 3 3

4 4 4 5 5 5

The unpredictability of the output is a direct result of not synchronizing the printValue method. To see this and get the desired result let the printValue () method be declared synchronized.

synchronized void printValue ()

Now, only one thread can execute in the method printValue ( ) and the result will be as

desired.

The following points are to be noted when implementing synchronized methods

• When a thread calls a non-static synchronized method, the thread acquires a lock on that

object. Any other thread that tries to call the same method enters into the wait state.

• The synchronization of static methods is independent of the synchronization of non-static methods. That is, if a thread calls a static synchronized method, the other threads are free to call non-static synchronized methods.

• A sub-class can override a synchronized method in its super-class. The overridden method need not be synchronized.

• If a non-synchronized method of an object uses the super () method to call a synchronized method of its super-class, the object is blocked until the invocation has completed.

Synchronized statements

As an alternative to declaring a method synchronized, Java shows flexibility. The programmer can declare a part of the code as synchronized using synchronized statement. The general syntax of declaring a set of statements synchronized is as follows:

synchronized <object><statement>

In the case of synchronized statements, we need to specify an object to act as a monitor for

this block. Each Java object can act as a monitor and hence any Java object can be specified synchronized.

In the example considered in Program we can achieve the desired output in another way, that is by declaring the method synchronized by using synchronized statement. For this the code is modified as in Program.

Program

import java.io.*;

class Class_A

{

    void printValue ()

   {

       try

     {

         for (int i =0; i<=5; i++)

        {

            System.out.print ( i +" ");

            Thread.sleep (1000);

        }

     }

      catch (InterruptedException e)

      {

       }

   }

}

class ThreadExample implements Runnable

{

    C1ass_A ob 1;

    Thread t;

    ThreadExample (Class_A c)

    {

         this.ob1 =c;

         t = new Thread (this);

    }

       public void run ()

       {

           synchronized (ob1)

           {

              obl.printValue ();

          }

      }

       public static void main (String [ ] args)

       {

          C1ass_A ca = new Class_A ();

          ThreadExample one=new ThreadExample (ca);

           one.t.start ();

           ThreadExample two=new ThreadExample (ca);

            two.t.start ();

           ThreadExample three=new ThreadExample (ca);

           three.t.start ();

      }

}

The output of Program is the following:

0 1 2 3 4 5 0 1 2 3 4 5

0 1 2 3 4 5

Deadlocks

Deadlocks occur on several occasions. These can be classified into one of the two following situations, namely, when two or more threads are waiting for two or more locks to be freed or circumstances in the program are such that the locks will never be freed.

Suppose one thread is a monitor of object A and another thread is monitor of the object B. In A and B are in the monitor because they are executing synchronized methods. At one instant, if A tries to access the synchronized method of B and B tries to access the synchronized method of A then they will enter into the Deadlock state.

If two or more threads are waiting for each other to unlock a synchronized block of code, where each thread relies on another to do the unlocking, then this situation is called deadlock situation. Deadlock situation occurs if the programmer is not careful while writing the synchronized methods.