• Java synchronized 锁住的是整个对象还是只有方法


    先上结论:

      类方法中,synchronized锁住的是对象this,只有调用同一个对象的方法才需要获取锁。同时,同一个对象中所有加了synchronize的方法只能一次调用一个

      静态方法中,synchronized锁的是整个类对象,类似于(X.class),该类中所有加了synchronized的静态方法,一次只能调用一个

     1 class Sync {
     2 
     3     public synchronized void test() {
     4         System.out.println("test开始..");
     5         try {
     6             Thread.sleep(1000);
     7         } catch (InterruptedException e) {
     8             e.printStackTrace();
     9         }
    10         System.out.println("test结束..");
    11     }
    12 
    13     public synchronized void test2() {
    14         System.out.println("test2开始..");
    15         try {
    16             Thread.sleep(800);
    17         } catch (InterruptedException e) {
    18             e.printStackTrace();
    19         }
    20         System.out.println("test2结束..");
    21     }
    22 }
    23 
    24 class MyThread extends Thread {
    25 
    26     private Sync sync;
    27 
    28     public MyThread(Sync sync) {
    29         this.sync = sync;
    30     }
    31 
    32     public void run() {
    33         sync.test();
    34     }
    35 }
    36 
    37 class MyThread2 extends Thread {
    38 
    39     private Sync sync;
    40 
    41     public MyThread2(Sync sync) {
    42         this.sync = sync;
    43     }
    44 
    45     public void run() {
    46         sync.test2();
    47     }
    48 }
    49 
    50 public class Main {
    51 
    52     public static void main(String[] args) {
    53         Sync sync = new Sync();
    54         Thread thread = new MyThread(sync);
    55         Thread thread2 = new MyThread2(sync);
    56         thread.start();
    57         thread2.start();
    58 
    59     }
    60 }

    运行结果:

    test开始..
    test结束..
    test2开始..
    test2结束..

     方案是按顺序执行的,说明了锁住的是同一个对象:

    main方法换成以下写法:

    1     public static void main(String[] args) {
    2         Thread thread = new MyThread(new Sync());
    3         Thread thread2 = new MyThread2(new Sync());
    4         thread.start();
    5         thread2.start();
    6 
    7     }

    结果:

    test开始..
    test2开始..
    test2结束..
    test结束..

    synchronized没有起到同步作用,说明不是同一个锁

    静态方法:

     1 class Sync {
     2 
     3     public static synchronized void test() {
     4         System.out.println("test开始..");
     5         try {
     6             Thread.sleep(1000);
     7         } catch (InterruptedException e) {
     8             e.printStackTrace();
     9         }
    10         System.out.println("test结束..");
    11     }
    12 
    13     public static synchronized void test2() {
    14         System.out.println("test2开始..");
    15         try {
    16             Thread.sleep(800);
    17         } catch (InterruptedException e) {
    18             e.printStackTrace();
    19         }
    20         System.out.println("test2结束..");
    21     }
    22 }
    23 
    24 class MyThread extends Thread {
    25 
    26     public void run() {
    27         Sync.test();
    28     }
    29 }
    30 
    31 class MyThread2 extends Thread {
    32 
    33     public void run() {
    34         Sync.test2();
    35     }
    36 }
    37 
    38 public class Main {
    39 
    40     public static void main(String[] args) {
    41         Thread thread = new MyThread();
    42         Thread thread2 = new MyThread2();
    43         thread.start();
    44         thread2.start();
    45 
    46     }
    47 }

    运行结果:

    test开始..
    test结束..
    test2开始..
    test2结束..

    本文参考了叉叉哥 的博客:Java线程同步:synchronized锁住的是代码还是对象

  • 相关阅读:
    谷歌翻译python接口
    SRILM的安装方法
    语言模型srilm基本用法
    SRILM语言模型格式解读
    矩阵理解
    python生成器 协程
    python Queue模块使用
    scrapy 学习笔记2
    scrapy 学习笔记1
    xpath语法规则
  • 原文地址:https://www.cnblogs.com/showstone/p/4438539.html
Copyright © 2020-2023  润新知