How Synchronization works in Java?

烈酒焚心 提交于 2019-12-17 18:28:55

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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
} 



回答6:


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.




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/11184642/how-synchronization-works-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!