synchronized关键字在多线程并发编程中一直是元老级角色的存在,是学习并发编程中必须面对的坎,也是走向Java高级开发的必经之路。

一、synchronized性质

synchronized是Java提供的内置锁机制,有如下两种特性:

  • 互斥性:即在同一时间最多只有一个线程能持有这种锁。当线程1尝试去获取一个由线程2持有的锁时,线程1必须等待或者阻塞,知道线程2释放这个锁。如果线程2永远不释放锁,那么线程1将永远等待下去。

  • 可重入性:即某个线程可以获取一个已经由自己持有的锁。

二、synchronized用法

Java中的每个对象都可以作为锁。根据锁对象的不同,synchronized的用法可以分为以下两种:

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

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

三、多线程访问同步方法的7种情况

本部分针对面试中常考的7中情况进行代码实战和原理解释。

1. 两个线程同时访问一个对象的同步方法

/**
* 两个线程同时访问一个对象的同步方法
*/
public class Demo1 implements Runnable { static Demo1 instance = new Demo1(); @Override
public void run() {
fun();
} public synchronized void fun() {
System.out.println(Thread.currentThread().getName() + "开始运行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行结束");
} public static void main(String[] args) {
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) { }
System.out.println("finished");
}
}

结果:两个线程顺序执行。

解释:thread1和thread2共用一把锁instance;同一时刻只能有一个线程获取锁;thread1先启动,先获得到锁,先运行,此时thread2只能等待。当thread1释放锁之后,thread2获取到锁,进行执行。

2. 两个线程访问的是两个对象的同步方法

public class Demo2 implements Runnable{

    static Demo2 instance1 = new Demo2();
static Demo2 instance2 = new Demo2(); @Override
public void run() {
fun();
} public synchronized void fun() {
System.out.println(Thread.currentThread().getName() + "开始运行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行结束");
} public static void main(String[] args) {
Thread thread1 = new Thread(instance1);
Thread thread2 = new Thread(instance2);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) { }
System.out.println("finished");
}
}

结果: 两个线程并行执行。

解释:thread1使用的锁对象是instance1,thread2使用的锁对象是instance2,两个对象使用的锁对象不是同一个,所以线程之间互不影响,是并行执行的。

3. 两个线程访问的是synchronized的静态方法

public class Demo3 implements Runnable{

    static Demo3 instance1 = new Demo3();
static Demo3 instance2 = new Demo3(); @Override
public void run() {
fun();
} public static synchronized void fun() {
System.out.println(Thread.currentThread().getName() + "开始运行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行结束");
} public static void main(String[] args) {
Thread thread1 = new Thread(instance1);
Thread thread2 = new Thread(instance2);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) { }
System.out.println("finished");
}
}

结果:两个线程顺序执行。

解释:虽然两个线程使用了两个不同的instance实例,但是只要方法是静态的,对应的锁对象是同一把锁,需要先后获取到锁进行执行。

4. 同时访问同步方法与非同步方法

public class Demo4 implements Runnable {

    static Demo4 instance = new Demo4();

    @Override
public void run() {
if (Thread.currentThread().getName().equals("Thread-0")){
fun1();
}else{
fun2();
}
} public synchronized void fun1() {
System.out.println(Thread.currentThread().getName() + "开始运行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "fun1运行结束");
} public void fun2() {
System.out.println(Thread.currentThread().getName() + "fun2开始运行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行结束");
}
public static void main(String[] args) {
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) { }
System.out.println("finished");
}
}

结果:两个线程并行执行。

解释:synchronize的关键字只对fun1起作用,不会对其他方法造成影响。也就是说同步方法不会对非同步方法造成影响,两个方法并行执行。

5. 访问同一个对象的不同的普通同步方法

public class Demo5 implements Runnable {

    static Demo5 instance = new Demo5();

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("Thread-0")){
            fun1();
        }else{
            fun2();
        }
    }     public synchronized void fun1() {
        System.out.println(Thread.currentThread().getName() + "开始运行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "fun1运行结束");
    }     public synchronized void fun2() {
        System.out.println(Thread.currentThread().getName() + "fun2开始运行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");
    }     public static void main(String[] args) {
        Thread thread1 = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {         }
        System.out.println("finished");
    }
}

结果:顺序执行。

解释:两个方法共用了instance对象锁,两个方法无法同时运行,只能先后运行。

6. 同时访问静态synchronized和非静态的synchronized方法

public class Demo6 implements Runnable{

    static Demo6 instance = new Demo6();

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("Thread-0")){
            fun1();
        }else{
            fun2();
        }
    }     public static synchronized void fun1() {
        System.out.println(Thread.currentThread().getName() + "开始运行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "fun1运行结束");
    }     public synchronized void fun2() {
        System.out.println(Thread.currentThread().getName() + "fun2开始运行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");
    }     public static void main(String[] args) {
        Thread thread1 = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {         }
        System.out.println("finished");
    }
}

结果:两个线程并行执行

解释:有static关键字,锁的是类本身;没有static关键字,锁的是对象实例;锁不是同一把锁,两个锁之间是没有冲突的;所以两个线程可以并行执行。

7. 方法抛异常后,会释放锁

public class Demo7 implements Runnable{

    static Demo7 instance = new Demo7();

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("Thread-0")){
            fun1();
        }else{
            fun2();
        }
    }     public synchronized void fun1() {
        System.out.println(Thread.currentThread().getName() + "开始运行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        throw new RuntimeException();
        //System.out.println(Thread.currentThread().getName() + "fun1运行结束");
    }     public synchronized void fun2() {
        System.out.println(Thread.currentThread().getName() + "fun2开始运行");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");
    }     public static void main(String[] args) {
        Thread thread1 = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {         }
        System.out.println("finished");
    }
}

结果:thread1运行时遇到异常,并未运行结束,thread2开始运行,并运行至结束。

解释:方法抛出异常后,JVM自动释放锁。

8. 上述7种情况总结

3点核心思想:

  1. 一把锁只能同时被一个线程获取,没有拿到锁的线程必须等待。

  2. 每个实例都对应有自己的一把锁,不同实例之间互不影响;例外:锁对象是.class以及synchronized修饰的是static方法的时候,所有对象共用同一把锁。

  3. 无论是方法正常运行完毕或者方法抛出异常,都会释放锁。

四、synchronized和ReentrantLock比较

虽然ReentrantLock是更加高级的锁机制,但是synchronized依然存在着如下的优点:

  1. synchronized作为内置锁为更多的开发人员所熟悉,代码简洁;

  2. synchronized较ReentrantLock更加安全,ReentrantLock如果忘记在finally中释放锁,虽然代码表面上运行正常,但实际上已经留下了隐患

  3. synchronized在线程转储中能给出在哪些调用帧中获得了哪些琐,并能够检测和识别发生死锁的线程。

五、总结

  1. synchronized关键字是Java提供的一种互斥的、可重入的内置锁机制。

  2. 其有两种用法:对象锁和类锁。

  3. 虽然synchronized与高级锁相比有着不够灵活、效率低等不足,但也有自身的优势:安全,依然是并发编程领域不得不学习的重要知识点。

用代码说话:synchronized关键字和多线程访问同步方法的7种情况的更多相关文章

  1. JAVA之旅(十三)——线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this

    JAVA之旅(十三)--线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this 我们继续上个篇幅接着讲线程的知识点 一.线程的安全性 当我们开启四个窗口(线程 ...

  2. synchronized关键字简介 多线程中篇(十一)

    前面说过,Java对象都有与之关联的一个内部锁和监视器 内部锁是一种排它锁,能够保障原子性.可见性.有序性 从Java语言层面上说,内部锁使用synchronized关键字实现 synchronize ...

  3. Synchronized关键字与多线程

    在java中,每一个对象有且仅有一个同步锁.这也意味着,同步锁是依赖于对象而存在.当我们调用某对象的synchronized方法时,就获取了该对象的同步锁.例如,synchronized(obj)就获 ...

  4. java之结合代码理解synchronized关键字

    为了保证数据的一致性即实现线程的安全性,java虚拟机提供了同步和锁机制.synchronized关键字是最基本的互斥同步手段.除此之外,还可以使用java.util.concurrent包中的重入锁 ...

  5. tomcat 访问400 的一种情况

    tomcat 高版本对访问url做了较高的校验,如果url中包含特殊字符,tomcat会自动拦截,返回400错误.如果要包含特殊字符,需要事先进行转译. 我原来用的apache-tomcat-6.0. ...

  6. Synchronized关键字整理

    Synchronized关键字整理 作用:能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全效果. 两个用法: 1.对象锁: 包括方法锁(默认锁对象为this当前实例对象)和同步代码块 ...

  7. Java 多线程 —— synchronized关键字

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...

  8. Java多线程学习(二)synchronized关键字(2)

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79670775 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  9. Java多线程学习(二)synchronized关键字(1)

    转载请备注地址: https://blog.csdn.net/qq_34337272/article/details/79655194 Java多线程学习(二)将分为两篇文章介绍synchronize ...

随机推荐

  1. Learning the Depths of Moving People by Watching Frozen

    基于双目的传统算法 对静止的物体, 在不同的 viewpoints 同一时刻进行拍摄, 根据拍摄到的结果, 使用三角测量算法计算出平面 2D 图像在 3D 图像中的坐标 单目 Ground Truth ...

  2. error: 'commit' is not possible because you have unmerged files.

    解决方案: 1.把修改的文件add下,如:git add bidder_mod/src/common/dragon_bidder_data.cc2.git commit

  3. 艺赛旗RPA-处理无表头表格

    今天写一个demo,要求是对表格数据用价格为key进行排序 样本数据有两种格式: 一.第一行是一个大单元格 处理步骤: 在不变参数的情况下读取表格数据: 结果如下: 可以看见表头: Unnamed: ...

  4. mui.storage 将数据持久化到本地

    在一个用mui做得app中,要求把历史记录放在本地(感觉...无法言喻的sd),但最终还是做了,以下来记录本次的学习到的内容 mui.plusReady(function() { //这里是一开始定义 ...

  5. 如何处理MySQL经常出现CPU占用率达到99%

    如何处理MySQL经常出现CPU占用率达到99% 情况说明: 最近在自己购买的linux服务器上捣鼓了一个小项目,按理说不存在CPU占用率会达到100%的情况,但事实就是经常出现. 然后,我第一反应是 ...

  6. @GetMapping、@PostMapping和@RequestMapping的区别

    @GetMapping 用于将Http Get 请求映射到特定处理程序方法的注释.具体来说就是:@GetMapping是一个作为快捷方式的组合注释 @RequestMapping(method = R ...

  7. java练习---6

    //程序员:罗元昊 2017.9.24 import java.util.Scanner; public class L { public static void main(String[] args ...

  8. Linux系统管理----磁盘管理与文件系统

    1.为主机新增两块30GB的SCSI硬盘 找到要添加的虚拟机,单击鼠标右键,点击设置 点击添加 选择硬件类型,然后点击下一步 选择要创建的磁盘类型,然后点击下一步 指定要创建磁盘的容量,然后点击下一步 ...

  9. python redis连接 有序集合去重

    # -*- coding: utf-8 -*- import redisfrom constant import redis_ip, redis_db, redis_pw, logger, redis ...

  10. 使用vue实现行列转换的一种方法。

    行列转换是一个老生常谈的问题,这几天逛知乎有遇到了这个问题.一个前端说,拿到的数据是单列的需要做转换才能够绑定,折腾了好久才搞定,还说这个应该后端直接出数据,不应该让前端折腾. 这个嘛,行列转换在后端 ...