多线程 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));
            }
        }

    }
}

 

posted @ 2018-08-25 16:42  newlangwen  阅读(1182)  评论(0编辑  收藏  举报