Java并发之synchronized用法及原理

2019-08-22  本文已影响0人  亚武de小文

一、synchronized简介

能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全的效果(当被sync修饰之后,将会以原子方式执行)

地位

二、synchronized两种用法

1、对象锁

对象锁:包括方法锁(默认锁对象为this当前实例对象)和同步代码块锁(自己制定锁对象)

生命周期

Debug调试:


Debug调试查看线程所处状态.png
代码实战
/**
 * 对象锁:代码块
 */
public class SynchronizedObjectCodeBlock02 implements Runnable {

    static SynchronizedObjectCodeBlock02 instance = new SynchronizedObjectCodeBlock02();

    Object lock1 = new Object();
    Object lock2 = new Object();

    @Override
    public void run() {
        // 同一个对象,则运行结果同this
        synchronized (lock1) {

            System.out.println("我是对象锁的代码块形式,我是Lock1,我叫" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "Lock1部分,运行结束。");
        }

        // 两把锁会出现并行,若为同一把锁,则串行
        synchronized (lock2) {

            System.out.println("我是对象锁的代码块形式,我是Lock2,我叫" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "Lock2部分,运行结束。");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while(t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("Finished");
    }
}

/**
 * 对象锁:代码块
 */
public class SynchronizedObjectMethod03 implements Runnable {

    static SynchronizedObjectMethod03 instance = new SynchronizedObjectMethod03();

    Object lock1 = new Object();
    Object lock2 = new Object();

    @Override
    public void run() {
        try {
            method();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void method() throws InterruptedException {
        System.out.println("我是对象所得方法修饰符形式,我叫" + Thread.currentThread().getName());
        Thread.sleep(3000);
        System.out.println(Thread.currentThread().getName() + ",运行结束。");
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while(t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("Finished");
    }
}

2、类锁

类锁:指synchronized修饰静态的方法或指定锁对象为Class对象

概念
两种形式
/**
 * 类锁的第一种形式,static形式
 */
public class SynchronizedClassStatic04 implements Runnable {
    static SynchronizedClassStatic04 instance1 = new SynchronizedClassStatic04();
    static SynchronizedClassStatic04 instance2 = new SynchronizedClassStatic04();

    @Override
    public void run() {
        try {
            method();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 此处不加static,会并行,加static会顺序串行执行
     * @throws InterruptedException
     */
    public static synchronized void method() throws InterruptedException {
        System.out.println("我是类锁的第一种形式static形式,我叫" + Thread.currentThread().getName());
        Thread.sleep(3000);
        System.out.println(Thread.currentThread().getName() + ",运行结束。");
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance1);
        Thread t2 = new Thread(instance2);
        t1.start();
        t2.start();
        while(t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("Finished");
    }
}

/**
 * 类锁的第一种形式,static形式
 */
public class SynchronizedClassClass05 implements Runnable {
    static SynchronizedClassClass05 instance1 = new SynchronizedClassClass05();
    static SynchronizedClassClass05 instance2 = new SynchronizedClassClass05();

    @Override
    public void run() {
        try {
            method();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void method() throws InterruptedException {
        /**
         * 此处若为this,则并行,*.class则串行
         */
        synchronized (SynchronizedClassClass05.class) {

            System.out.println("我是类锁的第二种形式static形式:synchronized(*.class),我叫" + Thread.currentThread().getName());
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName() + ",运行结束。");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance1);
        Thread t2 = new Thread(instance2);
        t1.start();
        t2.start();
        while(t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("Finished");
    }
}

三、性质

可重入性

指的是同一线程的外层函数获得锁之后,内层函数可以直接再次获取该锁

好处:避免死锁,提升封装性

粒度:线程而非调用(用3种情况来说明和pthread的区别)
3种情况:
1、证明同一个方法是可重入的
2、证明可重入不要求是同一个方法
3、证明可重入不要求是同一个类中的

不可中断性

一旦这个锁被别人获得了,如果我还想获得,我只能选择等待或者阻塞,直到别的线程释放这个锁,如果别人永远不释放锁,那么我只能永远的等下去

相比之下,Lock类,可以拥有中断的能力,第一点:如果我觉得我等的时间太长了,有权中断现在已经获取到锁的线程的执行;第二点:如果我觉得我等的时间太长了,不想再等了,可以退出

四、Synchronized原理

加锁和释放锁的原理:现象、时机、深入JVM看字节码
可重入原理:加锁次数计数器
保证可见性的原则:内存模型

1、加锁和释放锁的原理

深入JVM看字节码
/**
 * 反编译字节码
 */
public class Decompilation4 {
    private Object object = new Object();

    public void insert(Thread thread) {
        synchronized (object) {

        }
    }
}

javac Decompilation4.java
javap -verose Decompilation4.class

看到如下条目6、8、14


image.png

2、可重入原理:加锁次数计数器

3、可见性的原理:Java内存模型

线程间通信图.png

Synchronized修饰的代码块,在锁释放之前写入到主内存中,保证了本地内存和主内存一致性。

五、Synchronized缺陷

以上内容整理自网络

上一篇 下一篇

猜你喜欢

热点阅读