Call a block of a method only for the first call of the method

后端 未结 6 1723
后悔当初
后悔当初 2021-01-20 02:54

I have a method and inside this method I have a block:

public void method()
{
   [block instructions]
}

But this method is called twice in

6条回答
  •  感情败类
    2021-01-20 03:10

    private static final AtomicBoolean hasRunAtom = new AtomicBoolean();
    
    public void method() {
      if (hasRunAtom.getAndSet(true)) return;
      [block instructions]
    }
    

提交回复
热议问题