2.2.8细化验证3个结论

synchronized(非this对象x)格式的写法是将对象本身作为对象监视器,这样就可以得出以下3个结论。

1)当多个对象同时执行synchronized(x){}同步代码块是呈同步效果

2)当其他线程执行x对象中的synchronized同步方法呈现同步效果

3)当其他线程执行x对象方法里面的synchronized(this)代码块时呈同步效果

注:如果其他线程调用不加synchronized关键字的方法时,还是异步调用

验证1)

package com.cky.bean;

/**
 * Created by edison on 2017/12/3.
 */
public class MyObject {
}
package com.cky.bean;

/**
 * Created by edison on 2017/12/8.
 */
public class Service {

    public void testMethod1(MyObject object) {
        synchronized (object) {
            try {
                System.out.println("testMethod1 get lock "+ System.currentTimeMillis()
                        +"threadname"+Thread.currentThread().getName());

                Thread.sleep(2000);
                System.out.println("testMethod1 release lock "+ System.currentTimeMillis()+
                "threadname"+ Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
package com.cky.thread;

import com.cky.bean.MyObject;
import com.cky.bean.Service;

/**
 * Created by edison on 2017/12/8.
 */
public class ThreadA extends  Thread{

    private Service service;
    private MyObject object;

    public ThreadA(Service service, MyObject object) {
        this.service = service;
        this.object = object;
    }

    @Override
    public void run() {
        super.run();
        service.testMethod1(object);
    }
}
package com.cky.thread;

import com.cky.bean.MyObject;
import com.cky.bean.Service;

/**
 * Created by edison on 2017/12/8.
 */
public class ThreadB extends  Thread{
    private Service service;
    private MyObject object;

    public ThreadB(Service service, MyObject object) {
        this.service = service;
        this.object = object;
    }

    @Override
    public void run() {
        super.run();
        service.testMethod1(object);
    }
}
package com.cky.test;

import com.cky.bean.MyObject;
import com.cky.bean.Service;
import com.cky.thread.ThreadA;
import com.cky.thread.ThreadB;

/**
 * Created by edison on 2017/12/8.
 */
public class Test {
    public static void main(String[] args) {
        MyObject myObject = new MyObject();
        Service service = new Service();
        ThreadA threadA = new ThreadA(service, myObject);
        threadA.setName("a");
        threadA.start();
        ThreadB threadB = new ThreadB(service, myObject);
        threadB.setName("b");
        threadB.start();
    }
}
C:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
testMethod1 get lock 1512737427975threadnamea
testMethod1 release lock 1512737429975threadnamea
testMethod1 get lock 1512737429975threadnameb
testMethod1 release lock 1512737431976threadnameb

结果显示:同步,因为他们使用了同一个对象监视器,如果对象监视器不同,则显示结果也就异步了。

package com.cky.test;

import com.cky.bean.MyObject;
import com.cky.bean.Service;
import com.cky.thread.ThreadA;
import com.cky.thread.ThreadB;

/**
 * Created by edison on 2017/12/8.
 */
public class Test {
    public static void main(String[] args) {
        MyObject myObject1 = new MyObject();
        MyObject myObject2 = new MyObject();
        Service service = new Service();
        ThreadA threadA = new ThreadA(service, myObject1);
        threadA.setName("a");
        threadA.start();
        ThreadB threadB = new ThreadB(service, myObject2);
        threadB.setName("b");
        threadB.start();
    }
}
C:itsoftjdkinjava -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
testMethod1 get lock 1512737635943threadnamea
testMethod1 get lock 1512737635943threadnameb
testMethod1 release lock 1512737637955threadnamea
testMethod1 release lock 1512737637955threadnameb

Process finished with exit code 0

 验证(2)

package com.cky.bean;

/**
 * Created by edison on 2017/12/3.
 */
public class MyObject {
    synchronized public void speedPrintString(){
        System.out.println("speedprint getLock time" + System.currentTimeMillis() + " threadname=" +
                Thread.currentThread().getName());
        System.out.println("___________________");
        System.out.println("speedprint releaseLock time" + System.currentTimeMillis() + " threadname=" +
                Thread.currentThread().getName());

    }
}
package com.cky.bean;

/**
 * Created by edison on 2017/12/8.
 */
public class Service {

    public void testMethod1(MyObject object) {
        synchronized (object) {
            try {
                System.out.println("testMethod1 get lock "+ System.currentTimeMillis()
                        +"threadname"+Thread.currentThread().getName());

                Thread.sleep(2000);
                System.out.println("testMethod1 release lock "+ System.currentTimeMillis()+
                "threadname"+ Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
package com.cky.thread;

import com.cky.bean.MyObject;
import com.cky.bean.Service;

/**
 * Created by edison on 2017/12/8.
 */
public class ThreadA extends  Thread{

    private Service service;
    private MyObject object;

    public ThreadA(Service service, MyObject object) {
        this.service = service;
        this.object = object;
    }

    @Override
    public void run() {
        super.run();
        service.testMethod1(object);
    }
}
package com.cky.thread;

import com.cky.bean.MyObject;
import com.cky.bean.Service;

/**
 * Created by edison on 2017/12/8.
 */
public class ThreadB extends  Thread{
    private MyObject object;

    public ThreadB(Service service, MyObject object) {
        this.object = object;
    }

    @Override
    public void run() {
        super.run();
        object.speedPrintString();
    }
}
package com.cky.test;

import com.cky.bean.MyObject;
import com.cky.bean.Service;
import com.cky.thread.ThreadA;
import com.cky.thread.ThreadB;

/**
 * Created by edison on 2017/12/8.
 */
public class Test {
    public static void main(String[] args) {
        MyObject myObject = new MyObject();
        Service service = new Service();
        ThreadA threadA = new ThreadA(service, myObject);
        threadA.setName("a");
        threadA.start();
        ThreadB threadB = new ThreadB(service, myObject);
        threadB.setName("b");
        threadB.start();
    }
}
C:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
testMethod1 get lock 1512737929269threadnamea
testMethod1 release lock 1512737931269threadnamea
speedprint getLock time1512737931269 threadname=b
___________________
speedprint releaseLock time1512737931269 threadname=b

Process finished with exit code 0





3)验证第3个结论
package com.cky.bean;

/**
 * Created by edison on 2017/12/3.
 */
public class MyObject {
     public void speedPrintString(){
        synchronized (this) {
            System.out.println("speedprint getLock time" + System.currentTimeMillis() + " threadname=" +
                    Thread.currentThread().getName());
            System.out.println("___________________");
            System.out.println("speedprint releaseLock time" + System.currentTimeMillis() + " threadname=" +
                    Thread.currentThread().getName());
        }
       

    }
}

更改如上运行

C:itsoftjdkinjava -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
testMethod1 get lock 1512738280991threadnamea
testMethod1 release lock 1512738283002threadnamea
speedprint getLock time1512738283002 threadname=b
___________________
speedprint releaseLock time1512738283002 threadname=b




原文地址:https://www.cnblogs.com/edison20161121/p/8000439.html