synchronized(this) 和synchronized一样,都是锁定当前对象。

public class Task {

    synchronized public void otherMethod(){
        System.out.println("--------run otherMethod");
    }

    public void doLongTimeTask(){
        synchronized (this){
            for (int i=0;i<10000;i++){
                System.out.println("synchronized threadName="+Thread.currentThread().getName()+" i="+(i+1));
            }
        }

    }
}

 

相关文章:

  • 2022-12-23
  • 2021-12-12
  • 2022-01-13
  • 2022-01-29
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
相关资源
相似解决方案