Raj
Raj

Reputation: 2493

How Synchronization works in Java?

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

Upvotes: 21

Views: 18161

Answers (8)

Farhaz Malik
Farhaz Malik

Reputation: 41

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.

Upvotes: 0

user207421
user207421

Reputation: 310913

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.

Upvotes: -1

bezmax
bezmax

Reputation: 26132

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.

Upvotes: 20

Pramod Kumar
Pramod Kumar

Reputation: 8014

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.

Upvotes: 0

Johannes Weiss
Johannes Weiss

Reputation: 54031

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
} 

Upvotes: 0

Ahmad
Ahmad

Reputation: 2190

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.

Upvotes: 0

Miquel
Miquel

Reputation: 15675

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.

Upvotes: 0

Ajinkya
Ajinkya

Reputation: 22710

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

Upvotes: 1

Related Questions