2.1.4synchronized方法與對(duì)象鎖

/**
 * @author wuyoushan
 * @date 2017/3/29.
 */
public class MyObject {
    public void methodA(){
        try {
            System.out.println("begin methodA threadName=" +
                    Thread.currentThread().getName());
            Thread.sleep(1000);
            System.out.println("end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

/**
 * @author wuyoushan
 * @date 2017/4/4.
 */
public class ThreadA extends Thread{

    private MyObject object;

    public ThreadA(MyObject object){
        super();
        this.object=object;
    }

    @Override
    public void run() {
        super.run();
       object.methodA();
    }
}

/**
 * @author wuyoushan
 * @date 2017/4/4.
 */
public class ThreadB extends Thread{

    private MyObject object;

    public ThreadB(MyObject object){
        super();
        this.object=object;
    }

    @Override
    public void run() {
        super.run();
        object.methodA();
    }
}

/**
 * @author wuyoushan
 * @date 2017/3/20.
 */
public class Run {
    public static void main(String[] args){
        MyObject object=new MyObject();
        ThreadA threadA=new ThreadA(object);
        threadA.setName("A");

        ThreadB threadB=new ThreadB(object);
        threadB.setName("B");
        threadA.start();
        threadB.start();
    }
}

程序運(yùn)行候的結(jié)果為:

begin methodA threadName=A
begin methodA threadName=B
end
end

更改MyObject.java中的代碼:

/**
 * @author wuyoushan
 * @date 2017/3/29.
 */
public class MyObject {
    synchronized public void methodA(){
        try {
            System.out.println("begin methodA threadName=" +
                    Thread.currentThread().getName());
            Thread.sleep(1000);
            System.out.println("end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

更改后得運(yùn)行結(jié)果為:

begin methodA threadName=A
end
begin methodA threadName=B
end

通過上面的實(shí)驗(yàn)結(jié)論,調(diào)用關(guān)鍵字synchronized聲明的方法一定是排隊(duì)運(yùn)行的。另外需要牢牢記住“共享”這兩個(gè)字,只有共享資源的讀寫訪問才需要同步化,如果不是共享資源,那么根本就沒有同步的必要。

那其他的方法被調(diào)用時(shí)會(huì)是什么效果呢?如何查看到Lock鎖對(duì)象效果呢?

/**
 * @author wuyoushan
 * @date 2017/3/29.
 */
public class MyObject {
    synchronized public void methodA(){
        try {
            System.out.println("begin methodA threadName=" +
                    Thread.currentThread().getName());
            Thread.sleep(1000);
            System.out.println("end endTime="+System.currentTimeMillis());
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public void methodB(){
        try {
            System.out.println("begin methodB threadName=" +
                    Thread.currentThread().getName()+"begin time"+
            System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println("end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

/**
 * @author wuyoushan
 * @date 2017/4/4.
 */
public class ThreadA extends Thread{

    private MyObject object;

    public ThreadA(MyObject object){
        super();
        this.object=object;
    }

    @Override
    public void run() {
        super.run();
       object.methodA();
    }
}

/**
 * @author wuyoushan
 * @date 2017/4/4.
 */
public class ThreadB extends Thread{

    private MyObject object;

    public ThreadB(MyObject object){
        super();
        this.object=object;
    }

    @Override
    public void run() {
        super.run();
        object.methodB();
    }
}

/**
 * @author wuyoushan
 * @date 2017/3/20.
 */
public class Run {
    public static void main(String[] args){
        MyObject object=new MyObject();
        ThreadA threadA=new ThreadA(object);
        threadA.setName("A");

        ThreadB threadB=new ThreadB(object);
        threadB.setName("B");
        threadA.start();
        threadB.start();
    }
}

程序的運(yùn)行結(jié)果為:

begin methodA threadName=A
begin methodB threadName=Bbegin time1491525491404
end
end endTime=1491525492404

通過上面的實(shí)驗(yàn)可以得知,雖然線程A先持有了object對(duì)象的鎖,但線程B完全可以異步調(diào)用非synchronized類型的方法。

如果將MyObject.java文件中的methodB()方法前加上synchronized關(guān)鍵字,代碼如下:

/**
 * @author wuyoushan
 * @date 2017/3/29.
 */
public class MyObject {
    synchronized public void methodA(){
        try {
            System.out.println("begin methodA threadName=" +
                    Thread.currentThread().getName());
            Thread.sleep(1000);
            System.out.println("end endTime="+System.currentTimeMillis());
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    synchronized public void methodB(){
        try {
            System.out.println("begin methodB threadName=" +
                    Thread.currentThread().getName()+"begin time"+
            System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println("end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

程序的運(yùn)行結(jié)果為:

begin methodA threadName=A
end endTime=1491525875727
begin methodB threadName=Bbegin time1491525875727
end

此實(shí)驗(yàn)的結(jié)論是:

  1. A線程先持有object對(duì)象的Lock鎖,B線程可以以異步的方式調(diào)用object對(duì)象中的非synchronized類型的方法。
  2. A線程先持有object對(duì)象的Lock鎖,B線程如果在這時(shí)調(diào)用object對(duì)象中的synchronized類型的方法則需等待,也就是同步。

摘選自 java多線程核心編程技術(shù)-2.1.4

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容