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 t
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.