views:

39

answers:

4

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!

A: 

Synchronized methods locks the method on the object

synchronized void methodA () {
    ....    
}

is somehow equivalent to

void methodA () {
    synchronized (this) {
        ....
    }
}
nanda
+2  A: 

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.

Visage
A: 

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 class is the Class of the Object
fd
A: 

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

Fakrudeen