2.2.15内置类与同步:测试2

Posted 成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.2.15内置类与同步:测试2相关的知识,希望对你有一定的参考价值。

本实验测试同步代码块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:\itsoft\jdk\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_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是按同步执行的

 

以上是关于2.2.15内置类与同步:测试2的主要内容,如果未能解决你的问题,请参考以下文章

php 测试同步片段

缺少 SQL SERVER 2014 代码片段

[工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls(代码片段

exec模块,元类与ORV的应用

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段

Python 子类跟踪 - 元类与内置