0
6.4kviews
Write a short note on Thread Synchronization.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 5 M

Year: May 2014

1 Answer
0
23views
  • When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization.
  • For example, crediting and debiting a shared bank account concurrently amongst several users without proper discipline, will put a risk the integrity of the account data. Java provides high-level concepts for synchronization in order to control access to shared resources.
  • Key to synchronization is the concept of the monitor (also called a semaphore). A monitor is an object that is used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a given time.
  • When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor.
  • These other threads are said to be waiting for the monitor. A thread that owns a monitor can reenter the same monitor if it so desires.
  • The object lock mechanism forces the following rules of synchronization:
  1. A thread must acquire the object lock associated with a shared resource, before it can enter the shared resource. The runtime system ensures that no other thread can enter a shared resource if another thread already holds the object lock associated with the shared resource. If a thread cannot immediately acquire the object lock, it is blocked, that is, it must wait for the lock to become available.
  2. When a thread exits a shared resource, the runtime system ensures that the object lock is also handed over. If another thread is waiting for this object lock, it can proceed to acquire the lock in order to access to the shared resource.
  3. The keyword ‘synchronized’ and the lock forms the basis for implementing synchronized execution of code. There are two different ways in which execution of code can be synchronized:

    1) synchronized methods

    2) synchronized blocks

    1) Synchronized Methods

  4. As all objects have their own implicit monitor associated with them, synchronization is easy in Java. In order to enter an object’s lock, we just need to call a method that has been modified with the keyword ‘synchronized’.
  5. When a thread is running inside a synchronized method, all other threads that try to call it (or any other synchronized method) on the same instance they have to wait.
  6. To exit the lock and give up control of the object to the next waiting thread, the owner of that lock simply returns from the synchronized method.
  7. The syntax of synchronized method is as follows:

    class className

    {

    synchronized void methodName(parameters)

    {

    ………….

    …………. }

    }

    2) Synchronized blocks/statement

  • If we want to synchronize access to objects of a class that was not designed for multithreaded access. That is, the class does not use synchronized methods.
  • There can be situation where we may need to call methods of class whose source code can’t be accessed. Thus we cant add synchronized to those methods.
  • The solution to this is synchronized statement. We can put calls to the methods defined by this class inside a synchronized block.
  • This is the general form of the synchronized statement:

    synchronized(object) {

    // statements to be synchronized

    }

  • Here, object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object’s monitor.

Please log in to add an answer.