Tutorial :thread intrinsic lock



Question:

When we talk about intrinsic lock we refer to the object for which we ask the lock or for the synchronized method?

The lock is on the object or on it's sync method?

I'm confused!


Solution:1

Intrinsic locks are on the object:

class A  {     public synchronized void method1(){...}     public synchronized void method2(){...}  }  

If thread A is in method1 then threadB cannot enter method2.


Solution:2

In Java, an intrinsic lock is implied by each use of the synchronized keyword

Each use of the synchronized keyword is associated with one of the two types of intrinsic lock:

an "instance lock", attached to a single object

a "static lock", attached to a class

If a method is declared as synchronized, then it will acquire either the instance lock or the static lock when it is invoked, according to whether it is an instance method or a static method.

The two types of lock have similar behaviour, but are completely independent of each other.

Acquiring the instance lock only blocks other threads from invoking a synchronized instance method; it does not block other threads from invoking an un-synchronized method, nor does it block them from invoking a static synchronized method.

Similarly, acquiring the static lock only blocks other threads from invoking a static synchronized method; it does not block other threads from invoking an un-synchronized method, nor does it block them from invoking a synchronized instance method.

Outside of a method header, synchronized(this) acquires the instance lock.

The static lock can be acquired outside of a method header in two ways:

synchronized(Blah.class), using the class literal

synchronized(this.getClass()), if an object is available


Solution:3

Synchronized methods locks the method on the object

synchronized void methodA () {      ....      }  

is somehow equivalent to

void methodA () {      synchronized (this) {          ....      }  }  


Solution:4

The lock is part of the Object. Every Object has one and it may be locked in two ways:

  1. Using the synchronized modifier on an instance method of the Class to lock the associated object
  2. Using a synchronized(object) {} block

Similarly, you can lock the Class of an Object instead of the Object itself (bares mentioning separately to understand the synchronized modifier with a static method):

  1. Using the synchronized modifier on a static method of the Class to lock the Class
  2. Using a synchronized(clazz) {} block where clazz is the Class of the Object


Solution:5

Lock is on the object. In Java every object is a monitor


Solution:6

private int count = 0;  public synchronized void countFunc(){          count++;      }  Thread t1 = new Thread(new Runnable(){              public void run(){              for(int i=0;i<1000;i++){                  countFunc();                  }}});          Thread t2 = new Thread(new Runnable(){              public void run(){              for(int i=0;i<1000;i++){                  countFunc();              }}});  

In the above example, I have 2 threads trying to increment the value of count. And to prevent thread interleaving I am trying to grab an intrinsic lock by the use of synchronized keyword.

Conclusively, In this example, lock is the method block countFunc with the synchronized keyword and lock is on the count variable. Hope this helps


Solution:7

The lock is on Object.

Have a look at java tutorial page on intrinsic locks

Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock.

As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.

Two ways of using intrinsic locks:

  1. Synchronized methods:

    When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns.

    e.g.

    public synchronized void incrementCounter(){      ++counter;  }  
  2. Synchronized statements

    Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock

    public int getCounter(){      synchronized(this){          return counter;      }  }  

    Complete example:

    public class SynchronizedDemo{  private int counter = 0;    public SynchronizedDemo(){    }  public synchronized void incrementCounter(){      ++counter;  }  public int getCounter(){      synchronized(this){          return counter;      }  }  public static void main(String[] args){      SynchronizedDemo object = new SynchronizedDemo();      for ( int i=0; i < 5; i++){          Thread t = new Thread(new SimpleRunnable(object));          t.start();      }             }  }  class SimpleRunnable implements Runnable{  private SynchronizedDemo object;    public SimpleRunnable(SynchronizedDemo obj){      this.object = obj;  }  public void run(){      object.incrementCounter();      System.out.println("Counter:"+object.getCounter());  }  }  

Note: This example is written only to showcase different ways of using intrinsic locks. Using AtomicInteger for counter variable is right approach for this type of use case.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »