Locking and synchronization between outer and inner class methods?

后端 未结 3 1080
滥情空心
滥情空心 2020-12-04 01:33

My question is if I have some code like the following -:

public class OuterClass{
   public class InnerClass{
          public synchronized methodA(){ /* doe         


        
3条回答
  •  自闭症患者
    2020-12-04 02:05

    Since you have made the inner class method as synchronized it won't lock the outer class object rather it will only lock the inner class method. To synchronize the outer class as stated earlier it can done as shown below

    public void methA()
    {
    
    synchronized(OuterClass.this)
    
    {`enter code here`
            // ...
        }
    }
    

提交回复
热议问题