19

I have a doubt regarding Java Synchronization . I want to know if I have three Synchronized methods in my class and a thread acquires lock in one synchronized method other two will be locked ? I am asking this question because I am confused with the following statement .

While a thread is inside a synchronized method of an object, all other threads that wish to execute this synchronized method or any other synchronized method of the object will have to wait. This restriction does not apply to the thread that already has the lock and is executing a synchronized method of the object. Such a method can invoke other synchronized methods of the object without being blocked. The non-synchronized methods of the object can of course be called at any time by any thread

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Raj
  • 2,463
  • 10
  • 36
  • 52
  • 1
    What's the source of that statement and what's your confusion ? You seem to understand it. Be careful about deadlock when accessing one synchronized method from another. – Sridhar Jun 25 '12 at 06:57
  • Re: "...other two will be locked". The currently executing thread that already has the lock will not be prevented from calling another synchronized method on the same object, however any other thread will be blocked (i.e. forced to wait until they are given the lock). Max has a good answer that talks about whether you're locking on the object itself (i.e. this) or another object variable entirely. – Brad Jun 25 '12 at 11:05
  • one can understand the basics of synchronization from here https://dzone.com/articles/how-synchronization-works-in-java-part-1 – Abdul Mohsin Mar 13 '18 at 06:16

8 Answers8

18

Synchronization in java is done through aquiering the monitor on some specific Object. Therefore, if you do this:

class TestClass {
    SomeClass someVariable;

    public void myMethod () {
        synchronized (someVariable) {
            ...
        }
    }

    public void myOtherMethod() {
        synchronized (someVariable) {
            ...
        }
    }
}

Then those two blocks will be protected by execution of 2 different threads at any time while someVariable is not modified. Basically, it's said that those two blocks are synchronized against the variable someVariable.

When you put synchronized on the method, it basically means the same as synchronized (this), that is, a synchronization on the object this method is executed on.

That is:

public synchronized void myMethod() {
    ...
}

Means the same as:

public void myMethod() {
    synchronized (this) {
       ...
    }
}

Therefore, to answer your question - yes, threads won't be able to simultaneously call those methods in different threads, as they are both holding a reference to the same monitor, the monitor of this object.

bezmax
  • 25,562
  • 10
  • 53
  • 84
  • Max, the term "lock" in your last sentence is misleading. I think you meant to say "...as they are both holding a reference to the same monitor". Two threads cannot hold a lock from the same monitor at the same time. – Brad Jun 25 '12 at 08:53
  • 1
    Not getting what you have conveyed in first paragraph, can you please explain it simpler terms. – userab Apr 02 '17 at 21:41
  • as i understood it's only the synchronized methods that are being "locked". The non-synchronized methods of the object can be called at any time by any thread, as stated in the question implies that other threads can still call in parallel *or at the same time, the methods which are no synchronized – moldovean Jan 21 '18 at 08:44
  • @moldovean As I have explained in my answer, a "synchronized method" is nothing more than a shortcut to have a block that's synchronized on an object. – bezmax Jan 23 '18 at 02:59
1

Yes.
To execute synchronized method thread need to obtain lock on object and only one thread at a time can obtain lock on object.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

Each java object (class instance) has a mutex object. The synchronized keyword in front of a method means that the running thread has to get the lock on the mutex for that object. In fact,

public synchronized doSomething(){
   ...
}

Is exactly the same as this:

public  doSomething(){
   synchronized(this){
      ...
   }
}

So yes, there will only be one thread executing a synchronized method per class instance.

Note that sometimes this can be suboptimal, since you want to protect modifications, but are fine with concurrent reads, in which case, instead of the synchronized keyword, you might want to look into ReadWriteLock.

Miquel
  • 15,405
  • 8
  • 54
  • 87
0

well, there is only one lock per object. and all synchronized methods are locked by this lock. So , whichever thread acquires lock at a time, it is authorized to go through all synchronized methods. But the threads which waits for the lock can't enter into synchronize methods until they get the lock.

So at a time only only thread rules and others have to wait to enter any synchronized method, doesn't matter the ruling thread is executing that method or not.

Ahmad
  • 2,110
  • 5
  • 26
  • 36
0

Yes, all threads but the one which acquired the lock will have to wait until the lock gets released again to be able to execute one of the three synchronized methods.

Remember, a synchronized method is the same as a normal method surrounded by

synchronized(this) {
    // method body
} 
Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
0

It is true and it does in this way. It is necessary as well to consistent the data of that object.

Suppose that this validation is not there and there is a variable x which is being manipulated by 2 different synchronized method xxx() and yyy().

so if Thread A gets lock of method xxx() which is manipulating x=5 and second thread B gets lock of method yyy() and manipulating x=-5 so in the end of method xxx() thread A is expecting x=5 but it will get x=0 that is wrong.

Thats why it is implemented in this way.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
0

If a class has 4 synchronize methods, then yes at a time only one thread will have access to those methods. I guess doubt here was, each thread can access diff synchronized methods at a time for single class instance. The answer is no. Only one thread can access synchronized methods at a time.

-1

I'm not sure what it is you find confusing, but acquiring a lock blocks other threads from acquiring it while you hold it, and all non-static synchronized methods of a class synchronize on the same object, so the answer to your question is 'yes', assuming I have understood you correctly. I don't know what else 'synchronized' could mean, or what use it would be with any other meaning.

user207421
  • 305,947
  • 44
  • 307
  • 483