多线程 synchronized锁定当前对象

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));
            }
        }

    }
}
原文地址:https://www.cnblogs.com/newlangwen/p/9534595.html