Isabel Jinson
Isabel Jinson

Reputation: 8661

Synchronized makes object lock

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++;
      }    
   }

Upvotes: 3

Views: 459

Answers (5)

frictionlesspulley
frictionlesspulley

Reputation: 12368

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.

Upvotes: 0

psu
psu

Reputation: 379

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.

Upvotes: 0

Romain Hippeau
Romain Hippeau

Reputation: 24375

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.

Upvotes: 1

Kru
Kru

Reputation: 4235

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.

Upvotes: 1

danben
danben

Reputation: 83250

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.

Upvotes: 4

Related Questions