• Java多线程之8锁案例


    Case1.两个线程调用同一个对象的两个同步方法

            被synchronized修饰的方法,锁的对象是方法的调用者。因为两个方法的调用者是同一个,所以两个方法用的是同一个锁,先调用方法的先执行。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number number = new Number();
    4. new Thread(() -> number.getOne()).start();
    5. new Thread(() -> number.getTwo()).start();
    6. }
    7. }
    8. class Number {
    9. public synchronized void getOne() {
    10. System.out.println("one");
    11. }
    12. public synchronized void getTwo() {
    13. System.out.println("two");
    14. }
    15. }

    输出结果如下:

    one

    two

    Case2.新增Thread.sleep()给某个方法

            被synchronized修饰的方法,锁的对象是方法的调用者。因为两个方法的调用者是同一个,所以两个方法用的是同一个锁,先调用方法的先执行,第二个方法只有在第一个方法执行完释放锁之后才能执行。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number number = new Number();
    4. new Thread(() -> number.getOne()).start();
    5. new Thread(() -> number.getTwo()).start();
    6. }
    7. }
    8. class Number {
    9. public synchronized void getOne() {
    10. try {
    11. Thread.sleep(3000);
    12. } catch (InterruptedException e) {
    13. e.printStackTrace();
    14. }
    15. System.out.println("one");
    16. }
    17. public synchronized void getTwo() {
    18. System.out.println("two");
    19. }
    20. }

    输出结果如下:

    //等待3秒

    one

    two

    Case3.新增一个线程调用新增的一个普通方法

            新增的方法没有被synchronized修饰,不是同步方法,不受锁的影响,所以不需要等待。其他线程共用了一把锁,所以还需要等待。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number number = new Number();
    4. new Thread(() -> number.getOne()).start();
    5. new Thread(() -> number.getTwo()).start();
    6. new Thread(() -> number.getThree()).start();
    7. }
    8. }
    9. class Number {
    10. public synchronized void getOne() {
    11. try {
    12. Thread.sleep(3000);
    13. } catch (InterruptedException e) {
    14. e.printStackTrace();
    15. }
    16. System.out.println("one");
    17. }
    18. public synchronized void getTwo() {
    19. System.out.println("two");
    20. }
    21. public void getThree() {
    22. System.out.println("three");
    23. }
    24. }

    输出结果如下:

    three

    //等待3秒

    one

    two

    Case4.两个线程调用两个对象的同步方法,其中一个方法有Thread.sleep()

            被synchronized修饰的方法,锁的对象是方法的调用者。因为用了两个对象调用各自的方法,所以两个方法的调用者不是同一个,所以两个方法用的不是同一个锁,后调用的方法不需要等待先调用的方法。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number numberOne = new Number();
    4. Number numberTwo = new Number();
    5. new Thread(() -> numberOne.getOne()).start();
    6. new Thread(() -> numberTwo.getTwo()).start();
    7. }
    8. }
    9. class Number {
    10. public synchronized void getOne() {
    11. try {
    12. Thread.sleep(3000);
    13. } catch (InterruptedException e) {
    14. e.printStackTrace();
    15. }
    16. System.out.println("one");
    17. }
    18. public synchronized void getTwo() {
    19. System.out.println("two");
    20. }
    21. }

    输出结果如下:

    two

    //等待3秒

    one

    Case5.将有Thread.sleep()的方法设置为static方法,并且让两个线程用同一个对象调用两个方法

            被synchronized和static修饰的方法,锁的对象是类的class对象。仅仅被synchronized修饰的方法,锁的对象是方法的调用者。因为两个方法锁的对象不是同一个,所以两个方法用的不是同一个锁,后调用的方法不需要等待先调用的方法。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number number = new Number();
    4. new Thread(() -> number.getOne()).start();
    5. new Thread(() -> number.getTwo()).start();
    6. }
    7. }
    8. class Number {
    9. public static synchronized void getOne() {
    10. try {
    11. Thread.sleep(3000);
    12. } catch (InterruptedException e) {
    13. e.printStackTrace();
    14. }
    15. System.out.println("one");
    16. }
    17. public synchronized void getTwo() {
    18. System.out.println("two");
    19. }
    20. }

    输出结果如下:

    two

    //等待3秒

    one

    Case6.将两个方法均设置为static方法,并且让两个线程用同一个对象调用两个方法

            被synchronized和static修饰的方法,锁的对象是类的class对象。因为两个同步方法都被static修饰了,所以两个方法用的是同一个锁,后调用的方法需要等待先调用的方法。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number number = new Number();
    4. new Thread(() -> number.getOne()).start();
    5. new Thread(() -> number.getTwo()).start();
    6. }
    7. }
    8. class Number {
    9. public static synchronized void getOne() {
    10. try {
    11. Thread.sleep(3000);
    12. } catch (InterruptedException e) {
    13. e.printStackTrace();
    14. }
    15. System.out.println("one");
    16. }
    17. public static synchronized void getTwo() {
    18. System.out.println("two");
    19. }
    20. }

    输出结果如下:

    //等待3秒

    one

    two

    Case7.将两个方法中有Thread.sleep()的方法设置为static方法,另一个方法去掉static修饰,让两个线程用两个对象调用两个方法

            被synchronized和static修饰的方法,锁的对象是类的class对象。仅仅被synchronized修饰的方法,锁的对象是方法的调用者。即便是用同一个对象调用两个方法,锁的对象也不是同一个,所以两个方法用的不是同一个锁,后调用的方法不需要等待先调用的方法。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number numberOne = new Number();
    4. Number numberTwo = new Number();
    5. new Thread(() -> numberOne.getOne()).start();
    6. new Thread(() -> numberTwo.getTwo()).start();
    7. }
    8. }
    9. class Number {
    10. public static synchronized void getOne() {
    11. try {
    12. Thread.sleep(3000);
    13. } catch (InterruptedException e) {
    14. e.printStackTrace();
    15. }
    16. System.out.println("one");
    17. }
    18. public synchronized void getTwo() {
    19. System.out.println("two");
    20. }
    21. }

    输出结果如下:

    two

    //等待3秒

    one

    Case8.将两个方法均设置为static方法,并且让两个线程用同一个对象调用两个方法

            被synchronized和static修饰的方法,锁的对象是类的class对象。因为两个同步方法都被static修饰了,即便用了两个不同的对象调用方法,两个方法用的还是同一个锁,后调用的方法需要等待先调用的方法。

    1. public class Demo {
    2. public static void main(String[] args) {
    3. Number numberOne = new Number();
    4. Number numberTwo = new Number();
    5. new Thread(() -> numberOne.getOne()).start();
    6. new Thread(() -> numberTwo.getTwo()).start();
    7. }
    8. }
    9. class Number {
    10. public static synchronized void getOne() {
    11. try {
    12. Thread.sleep(3000);
    13. } catch (InterruptedException e) {
    14. e.printStackTrace();
    15. }
    16. System.out.println("one");
    17. }
    18. public static synchronized void getTwo() {
    19. System.out.println("two");
    20. }
    21. }

    输出结果如下:

    //等待3秒

    one

    two

    小结

    • 一个类里面如果有多个synchronized方法,在使用同一个对象调用的前提下,某一个时刻内,只要一个线程去调用其中的一个synchronized方法了,其他的线程都只能等待,换句话说,某一时刻内,只能有唯一一个线程去访问这些synchronized方法。
    • 锁的是当前对象this,被锁定后,其他线程都不能进入到当前对象的其他的synchronized方法。
    • 加个普通方法后发现和同步锁无关。
    • 换成静态同步方法后,情况又变化。
    • 所有的非静态同步方法用的都是同一把锁:实例对象本身。
    • 也就是说如果一个对象的非静态同步方法获取锁后,该对象的其他非静态同步方法必须等待获取锁的方法释放锁后才能获取锁,可是其他对象的非静态同步方法因为跟该对象的非静态同步方法用的是不同的锁,所以毋须等待该对象的非静态同步方法释放锁就可以获取他们自己的锁。
    • 所有的静态同步方法用的也是同一把锁:类对象本身。
    • 这两把锁是两个不同的对象,所以静态同步方法与非静态同步方法之间不会有竞争条件。但是一旦一个静态同步方法获取锁后,其他的静态同步方法都必须等待该方法释放锁后才能获取锁,而不管是同一个对象的静态同步方法,还是其他对象的静态同步方法,只要它们属于同一个类的对象,那么就需要等待当前正在执行的静态同步方法释放锁。
  • 相关阅读:
    数据结构——顺序表
    【解决Win】“ 无法打开某exe提示无法成功完成操作,因为文件包含病毒或潜在的垃圾软件“
    win10电脑右键新建没有记事本的解决方法
    QT QScrollArea控件 使用详解
    《痞子衡嵌入式半月刊》 第 100 期
    计算机毕业设计Java哈尔滨旅游项目推荐平台演示录像2020(源码+系统+mysql数据库+Lw文档)
    夸克扫描王识别精度领跑行业 愿携手各方伙伴探索AIGC应用新范式
    【云原生之kubernetes实战】kubernetes集群下的存储持久化
    PTA题目 装睡
    vueRouter个人笔记
  • 原文地址:https://blog.csdn.net/baidu_35410857/article/details/134326450