reentrant synchronization

前端 未结 3 1969
一整个雨季
一整个雨季 2021-02-15 12:24

Can you tell me if following invocations are reentrant or not?

public class Foo {

  public synchronized void doSomething() {}

  public synchronized void doAnot         


        
3条回答
  •  走了就别回头了
    2021-02-15 13:07

    first of all locks are applicable on objects so need to create the object and then apply the lock.

    are 2 continuous invocations in method doToo() reentrant? I

    In your case they are not re-entrant . if code is something like below then they will be re-entrant .

      public class Foo {
    
     public synchronized void doSomething() {
            doAnotherSomething();
    }
    
     public synchronized void doAnotherSomething() {}
    }
    

    Once lock has been acquired on one object then on same object it can traverse like in above case.

提交回复
热议问题