2.2.8细化验证3个结论

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:\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.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:\itsoft\jdk\bin\java -Didea.launcher.port=7533 "-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.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:\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.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:\itsoft\jdk\bin\java -Didea.launcher.port=7534 "-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.test.Test
testMethod1 get lock 1512738280991threadnamea
testMethod1 release lock 1512738283002threadnamea
speedprint getLock time1512738283002 threadname=b
___________________
speedprint releaseLock time1512738283002 threadname=b

 















以上是关于2.2.8细化验证3个结论的主要内容,如果未能解决你的问题,请参考以下文章

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

结构思维

JS常用代码片段-127个常用罗列-值得收藏

16个必备的JavaScript代码片段

Vue友们就靠这6个开发技巧了(建议收藏)

JSP 设计教师与学生不同登陆界面(带验证码)