Synchronized Block within Synchronized Method

前端 未结 4 631
一生所求
一生所求 2021-01-02 22:39

I\'m looking at some code in a third party library that contains a synchronized method, and within this method there is a synchronized block that locks on an instance variab

4条回答
  •  余生分开走
    2021-01-02 23:08

    Consider this:

    public void someMethod() {
    
        synchronized(bar) {
           // fully accessible before entering the other synchronized bar block
           // but not afterwards 
        }
    
    }
    

    Get it clearly, synchronizing only blocks if 2 blocks synchronize on the same object.

提交回复
热议问题