本实验测试同步代码块synchronized(class2)对class2上锁后,其他线程只能以同步的方式调用class2中的静态同步方法
package com.cky.bean; /** * Created by edison on 2017/12/9. */ public class OuterClass { static class InnerClass1 { public void method1 (InnerClass2 class2) { String thname = Thread.currentThread().getName(); synchronized (class2) { System.out.println(thname + "进入InnerClass1中method1"); for (int i = 0; i < 10; i++) { System.out.println("i="+i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(thname +"离开InnerClass1中method1"); } } } public synchronized void method2() { String thname = Thread.currentThread().getName(); System.out.println(thname + "进入InnerClass1中method2"); for (int j = 0; j < 10; j++) { System.out.println("j="+j); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(thname +"离开InnerClass1中method2"); } } } static class InnerClass2 { public synchronized void method1() { String thname = Thread.currentThread().getName(); System.out.println(thname + "进入InnerClass2中method1"); for (int k = 0; k < 10; k++) { System.out.println("k="+k); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(thname +"离开InnerClass2中method1"); } } } }
package com.cky.bean; /** * Created by edison on 2017/12/9. */ public class Test3 { public static void main(String[] args) { OuterClass.InnerClass1 innerClass1 = new OuterClass.InnerClass1(); OuterClass.InnerClass2 innerClass2 = new OuterClass.InnerClass2(); Thread t1 = new Thread(new Runnable() { @Override public void run() { innerClass1.method1(innerClass2); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { innerClass1.method2(); } }); Thread t3 = new Thread(new Runnable() { @Override public void run() { innerClass2.method1(); } }); t1.start(); t2.start(); t3.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.bean.Test3 Thread-0进入InnerClass1中method1 i=0 Thread-1进入InnerClass1中method2 j=0 Thread-0离开InnerClass1中method1 i=1 Thread-1离开InnerClass1中method2 j=1 Thread-0离开InnerClass1中method1 Thread-1离开InnerClass1中method2 j=2 i=2 Thread-1离开InnerClass1中method2 j=3 Thread-0离开InnerClass1中method1 i=3 Thread-0离开InnerClass1中method1 i=4 Thread-1离开InnerClass1中method2 j=4 Thread-1离开InnerClass1中method2 Thread-0离开InnerClass1中method1 i=5 j=5 Thread-0离开InnerClass1中method1 i=6 Thread-1离开InnerClass1中method2 j=6 Thread-1离开InnerClass1中method2 j=7 Thread-0离开InnerClass1中method1 i=7 Thread-0离开InnerClass1中method1 i=8 Thread-1离开InnerClass1中method2 j=8 Thread-0离开InnerClass1中method1 i=9 Thread-1离开InnerClass1中method2 j=9 Thread-0离开InnerClass1中method1 Thread-2进入InnerClass2中method1 k=0 Thread-1离开InnerClass1中method2 Thread-2离开InnerClass2中method1 k=1 Thread-2离开InnerClass2中method1 k=2 Thread-2离开InnerClass2中method1 k=3 Thread-2离开InnerClass2中method1 k=4 Thread-2离开InnerClass2中method1 k=5 Thread-2离开InnerClass2中method1 k=6 Thread-2离开InnerClass2中method1 k=7 Thread-2离开InnerClass2中method1 k=8 Thread-2离开InnerClass2中method1 k=9 Thread-2离开InnerClass2中method1 Process finished with exit code 0
结果分析
InnerClass1中method1和InnerClass2method1的锁对象是同一个,而InnerClass1中的method2的锁对象是InnerClass1
所以i和j是异步打印的,而i和k是按同步执行的