views:

112

answers:

5

I have a confusion about object lock. The below class having 4 methods, the method addB() is synchronized.

In my scienario, there are 4 threads. When a thread-2 access the addB() method (it creates a lock on Test object), will there any other thread access addC() or addD() same time?

Does the Object lock allows only one thread at a time ?

class Test{
       private Integer a;
       private Integer b;
       private Integer c;
       private Integer d;


   public void addA(){
      synchronized(a) {
         a++;
      }
   }
   public synchronized void addB(){
         b++;
      }

   public void addC(){
         c++;
      }

   public void addD(){
         d++;
      }    
   }

EDIT: I have 3 threads(t1, t2 and t3) , and each one is going to access addB(), addC() and addD(). If thread t1 access the method addB(), could thread t2 access addC() method simultaneously? If not what would be t2 state?

class Test{
       private Integer a;
       private Integer b;
       private Integer c;
       private Integer d;


   public void addA(){
      synchronized(a) {
         a++;
      }
   }
   public synchronized void addB(){
         b++;
      }

   public synchronized void addC(){
         c++;
      }

   public synchronized void addD(){
         d++;
      }    
   }
+4  A: 

A lock does indeed allow only one thread at a time, but different locks do not affect each other.

In your example, you have two locks - one on the mutex belonging to a, and one on the mutex belonging to this (which is implicit when you use the synchronized keyword, as you correctly mentioned in your post).

So calls to addB() will be synchronized but will not block calls to any other method. If one thread holds the lock on this, another thread can hold the lock on a, and multiple other threads can execute addC() and addD() concurrently.

Edit: as an aside, you might be interested to learn about the AtomicInteger class if you really are working with Integers. They provide atomic operations such that you don't need to worry about synchronizing around them.

danben
@danben, i guess then we should not tell that synchronization creates object lock, because other threads can access non synchronized method at object locks state. I'm still confused what does it mean object lock?
Thomman
@Thomman: I'm not sure what you mean by "at object locks state". Every Object in Java has its own mutex - when you synchronize on an Object, it is this mutex that needs to be held before a thread can proceed into the synchronized block.
danben
+1  A: 

You have two locks in your code, only one Thread will be able to traverse either Lock #1 or Lock #2. Both locks are independent, meaning they do not exclude Threads from each other.

Lock #1 synchronizes on the a Object

   public void addA(){
      synchronized(a) {
         a++;
      }
   }

lock #2 Synchronizes on the Test Instance (this)

   public synchronized void addB(){
         b++;
      }

addC() and addD() have no locks on them at all, any amount of Threads can access these concurrently.

Romain Hippeau
+1  A: 

A synchronized block is just an environment where you treat the object it was "executed" on as a reentrant lock. Indeed, only one thread is allowed locking on an object at the same time.

The methods C and D never lock and can be executed any time by any thread.

And as other pointed, when you execute a++, you create a new instance of Integer.

Chris
A: 

Lock provides a mechanism for synchronizing the thread. this means that at the same point in time only one thread can access the AddB method of your object. Unless you release the lock after completion of the code, the next code iteration cannot enter the block.

psu
A: 

Locking of thread depends on the instance of the object used e.g:

class MyClass extends Thread{
   Test instanceObj=null;    
   public MyClass(Test obj){
      instanceObj=obj;
   }

   public void run(){
      obj.addB();
   }
}

the addB() function can be rewriten as

public void addB(){
  synchronized(this){
    //func
 }
}
  • Here the lock is on the object for access the snippet of the function. other functions can be accessed by other threads.
  • More over the lock on addA is on an object obj.a which has its own indepedent lock.Hence two different threads can acccess addA and addB at the same time.
frictionlesspulley