博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
synchronized实现原理
阅读量:4182 次
发布时间:2019-05-26

本文共 5054 字,大约阅读时间需要 16 分钟。

一、synchronized作用于实例方法。

package com.xiaobu.note.Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/8 16:15 * @description V1.0  synchronized作用于实例方法 同一个对象 */public class Demo1 implements Runnable{    private static int count;    private synchronized void increase(){        count++;    }    @Override    public void run() {        for (int i=0;i<50000;i++){           increase();        }    }    /**     * 结果:count = 100000     * 两个线程访问的同一个对象     * 只有执行完该代码块才能释放该对象锁,下一个线程才能执行并锁定该对象。     */    public static void main(String[] args) throws InterruptedException {        Demo1 instance = new Demo1();        Thread thread1 = new Thread(instance);        Thread thread2 = new Thread(instance);        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println("count = " + count);    }}

二、修饰实例方法。

package Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/8 16:15 * @description V1.0  synchronized作用于实例方法 不同的对象 */public class Demo2 implements Runnable{    private static int count;    private synchronized void increase(){        count++;    }    @Override    public void run() {        for (int i=0;i<50000;i++){           increase();        }    }    /**     * 结果:count = 86136     *虽然使用synchronized修饰了increase方法,但却new了两个不同的实例对象,     * 这也就意味着存在着两个不同的实例对象锁     */    public static void main(String[] args) throws InterruptedException {        Thread thread1 = new Thread(new Demo2());        Thread thread2 = new Thread(new Demo2());        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println("count = " + count);    }}

三、修饰静态实例方法。

package Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/8 16:15 * @description V1.0  synchronized作用于静态实例方法 同个对象(类的class对象) */public class Demo3 implements Runnable{    private static int count;    private synchronized static void increase(){        count++;    }    @Override    public void run() {        for (int i=0;i<50000;i++){           increase();        }    }    /**     * 结果:count = 100000     *访问静态 synchronized 方法占用的锁是当前类的class对象     * 而访问非静态 synchronized 方法占用的锁是当前实例对象锁     */    public static void main(String[] args) throws InterruptedException {        Thread thread1 = new Thread(new Demo3());        Thread thread2 = new Thread(new Demo3());        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println("count = " + count);    }}

四、修饰同步代码块。

package Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/8 16:15 * @description V1.0  synchronized作用于同步代码块 效果与demo1等价 */public class Demo4 implements Runnable{    private static final Demo4 instance = new Demo4();    private static int count;    @Override    public void run() {        //将instance改成this也是一样的效果        synchronized (instance){            for (int i=0;i<50000;i++){                count++;            }        }    }    /**     * 结果:count = 100000     */    public static void main(String[] args) throws InterruptedException {        Thread thread1 = new Thread(instance);        Thread thread2 = new Thread(instance);        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println("count = " + count);    }}

 

package Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/8 16:15 * @description V1.0  synchronized作用于同步代码块 效果与demo2等价 */public class Demo5 implements Runnable{    private static int count;    @Override    public void run() {        //将instance改成this也是一样的效果        synchronized (Demo5.class){            for (int i=0;i<50000;i++){                count++;            }        }    }    /**     * 结果:count = 100000     */    public static void main(String[] args) throws InterruptedException {        Thread thread1 = new Thread(new Demo5());        Thread thread2 = new Thread(new Demo5());        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println("count = " + count);    }}

 

package com.xiaobu.JUC.Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/9/25 14:05 * @description 如果一个类中定义了一个synchronized的static函数A, *  也定义了一个synchronized的instance函数B, *  那么这个类的同一对象Obj,在多线程中分别访问A和B两个方法时,不会构成同步, *  因为它们的锁都不一样。B方法的锁是Obj这个对象,而A的锁是Obj所属的那个Class。 */public class SynchronizedDemo {    /**     *定义一个synchronized的static函数A     */    public static synchronized  void A(){    }    /**     *定义一个synchronized的instance函数B     */    public  synchronized  void B(){    }}

验证 synchronized为可重入锁

package com.xiaobu.JUC.Synchronized;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/9/24 16:37 * @description 验证 synchronized为可重入锁 */public class ReentrantTest {    public static void main(String[] args) {/*  A  B */        A();    }    public static synchronized void A() {        System.out.println("A");        B();    }    public static synchronized void B() {        System.out.println("B");    }}

 

转载地址:http://kfgai.baihongyu.com/

你可能感兴趣的文章
《Linux内核编程》第十四章:Linux驱动基础
查看>>
Linux平台下ARM-Linux交叉编译工具链
查看>>
Window平台下ADS自带ARMCC编译工具链
查看>>
micro2440/tiny6410使用JLINK直接烧录nand flash
查看>>
C编译器、连接器与可执行机器码文件
查看>>
android linker 浅析
查看>>
802.11 traffic id
查看>>
Android系统wifi分析-手动连接过程
查看>>
设置IP别名Shell脚本
查看>>
Source Insight 宏-单行注释
查看>>
levelDB源码分析-Arena
查看>>
levelDB源码分析-SSTable
查看>>
平滑升级Nginx的Shell脚本
查看>>
SSH远程会话管理工具
查看>>
canvas标签设长宽是在css中还是在标签中
查看>>
如何创建一个vue项目
查看>>
webpack和webpack-simple中如何引入css文件
查看>>
vue1.0和vue2.0的区别之路由
查看>>
关于vue-router2.0的学习笔记
查看>>
vue1.0与2.0区别之生命周期
查看>>