How Synchronization works in Java?

后端 未结 8 578
广开言路
广开言路 2020-12-08 21:46

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

相关标签:
8条回答
  • 2020-12-08 22:04

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

    0 讨论(0)
  • 2020-12-08 22:10

    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.

    0 讨论(0)
  • 2020-12-08 22:10

    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.

    0 讨论(0)
  • 2020-12-08 22:21

    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.

    0 讨论(0)
  • 2020-12-08 22:24

    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.

    0 讨论(0)
  • 2020-12-08 22:24

    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.

    0 讨论(0)
提交回复
热议问题