天天用Synchronized,底层原理是个啥?

简介: Synchronized 的基本使用

Synchronized 的基本使用


Synchronized 的作用主要有三个:


确保线程互斥的访问同步代码


保证共享变量的修改能够及时可见


有效解决重排序问题


从语法上讲,Synchronized 总共有三种用法:


修饰普通方法


修饰静态方法


修饰代码块


接下来我就通过几个例子程序来说明一下这三种使用方式(为了便于比较,三段代码除了 Synchronized 的使用方式不同以外,其他基本保持一致)。


没有同步的情况


代码段 1:

package com.paddx.test.concurrent;
public class SynchronizedTest {
  public void method1(){
    System.out.println("Method 1 start");
    try {
      System.out.println("Method 1 execute");
      Thread.sleep(3000);
    } catch (InterruptedException e) {
       e.printStackTrace();
    }
    System.out.println("Method 1 end");
  }
  public void method2(){
    System.out.println("Method 2 start");
    try {
      System.out.println("Method 2 execute");
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 2 end");
  }
  public static void main(String\[\] args) {
    final SynchronizedTest test = new SynchronizedTest();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method2();
      }
    }).start();
  }
}

执行结果如下,线程 1 和线程 2 同时进入执行状态,线程 2 执行速度比线程 1 快,所以线程 2 先执行完成。推荐阅读:多线程 start 和 run 方法到底有什么区别?

这个过程中线程 1 和线程 2 是同时执行的:

Method 1 start
Method 1 execute
Method 2 start
Method 2 execute
Method 2 end
Method 1 end

对普通方法同步

代码段 2:

package com.paddx.test.concurrent;
public class SynchronizedTest {
  public synchronized void method1(){
    System.out.println("Method 1 start");
    try {
      System.out.println("Method 1 execute");
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 1 end");
  }
  public synchronized void method2(){
    System.out.println("Method 2 start");
    try {
      System.out.println("Method 2 execute");
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 2 end");
  }
  public static void main(String\[\] args) {
    final SynchronizedTest test = new SynchronizedTest();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method2();
      }
    }).start();
  }
}

执行结果如下,跟代码段 1 比较,可以很明显的看出,线程 2 需要等待线程 1 的 Method1 执行完成才能开始执行 Method2 方法。

Method 1 start
Method 1 execute
Method 1 end
Method 2 start
Method 2 execute
Method 2 end

静态方法(类)同步

代码段 3:

package com.paddx.test.concurrent;
public class SynchronizedTest {
  public static synchronized void method1(){
    System.out.println("Method 1 start");
    try {
      System.out.println("Method 1 execute");
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 1 end");
  }
  public static synchronized void method2(){
    System.out.println("Method 2 start");
    try {
      System.out.println("Method 2 execute");
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 2 end");
  }
  public static void main(String\[\] args) {
    final SynchronizedTest test = new SynchronizedTest();
    final SynchronizedTest test2 = new SynchronizedTest();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test2.method2();
      }
    }).start();
  }
 }

执行结果如下,对静态方法的同步本质上是对类的同步(静态方法本质上是属于类的方法,而不是对象上的方法)。


所以即使 Test 和 Test2 属于不同的对象,但是它们都属于 SynchronizedTest 类的实例。


所以也只能顺序的执行 Method1 和 Method2,不能并发执行:

Method 1 start
Method 1 execute
Method 1 end
Method 2 start
Method 2 execute
Method 2 end

代码块同步

代码段 4:

package com.paddx.test.concurrent;
public class SynchronizedTest {
  public void method1(){
    System.out.println("Method 1 start");
    try {
      synchronized (this) {
        System.out.println("Method 1 execute");
        Thread.sleep(3000);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 1 end");
  }
  public void method2(){
    System.out.println("Method 2 start");
    try {
      synchronized (this) {
        System.out.println("Method 2 execute");
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Method 2 end");
  }
  public static void main(String\[\] args) {
    final SynchronizedTest test = new SynchronizedTest();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        test.method2();
      }
    }).start();
  }
}

执行结果如下,虽然线程 1 和线程 2 都进入了对应的方法开始执行,但是线程 2 在进入同步块之前,需要等待线程 1 中同步块执行完成。

Method 1 start
Method 1 execute
Method 2 start
Method 1 end
Method 2 execute
Method 2 end

Synchronized 原理


如果对上面的执行结果还有疑问,也先不用急,我们先来了解 Synchronized 的原理。推荐阅读:面试常考:Synchronized 有几种用法?


再回头上面的问题就一目了然了。我们先通过反编译下面的代码来看看 Synchronized 是如何实现对代码块进行同步的:

package com.paddx.test.concurrent;
public class SynchronizedMethod {
  public synchronized void method() {
    System.out.println("Hello World!");
  }
}

反编译结果:

image.png

关于这两条指令的作用,我们直接参考 JVM 规范中描述:


monitorenter :Each object is associated with a monitor. A monitor is locked if and only if it has an owner. The thread that executes monitorenter attempts to gain ownership of the monitor associated with objectref, as follows:


• If the entry count of the monitor associated with objectref is zero, the thread enters the monitor and sets its entry count to one. The thread is then the owner of the monitor.


• If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count.


• If another thread already owns the monitor associated with objectref, the thread blocks until the monitor's entry count is zero, then tries again to gain ownership.


这段话的大概意思为:每个对象有一个监视器锁(Monitor),当 Monitor 被占用时就会处于锁定状态。


线程执行 Monitorenter 指令时尝试获取 Monitor 的所有权,过程如下:


如果 Monitor 的进入数为 0,则该线程进入 Monitor,然后将进入数设置为 1,该线程即为 Monitor 的所有者。


如果线程已经占有该 Monitor,只是重新进入,则进入 Monitor 的进入数加 1。


如果其他线程已经占用了 Monitor,则该线程进入阻塞状态,直到 Monitor 的进入数为 0,再重新尝试获取 Monitor 的所有权。


monitorexit:The thread that executes monitorexit must be the owner of the monitor associated with the instance referenced by objectref.


The thread decrements the entry count of the monitor associated with objectref. If as a result the value of the entry count is zero, the thread exits the monitor and is no longer its owner.


Other threads that are blocking to enter the monitor are allowed to attempt to do so.


这段话的大概意思为:执行 Monitorexit 的线程必须是 Objectref 所对应的 Monitor 的所有者。


指令执行时,Monitor 的进入数减 1,如果减 1 后进入数为 0,那线程退出 Monitor,不再是这个 Monitor 的所有者。


其他被这个 Monitor 阻塞的线程可以尝试去获取这个 Monitor 的所有权。


通过这两段描述,我们应该能很清楚的看出 Synchronized 的实现原理。


Synchronized 的语义底层是通过一个 Monitor 的对象来完成,其实 Wait/Notify 等方法也依赖于 Monitor 对象。推荐阅读:Synchronized 与 ReentrantLock 的区别!


这就是为什么只有在同步的块或者方法中才能调用 Wait/Notify 等方法,否则会抛出 java.lang.IllegalMonitorStateException 的异常。


我们再来看一下同步方法的反编译结果,源代码如下:

image.png

从反编译的结果来看,方法的同步并没有通过指令 Monitorenter 和 Monitorexit 来完成(理论上其实也可以通过这两条指令来实现)。不过相对于普通方法,其常量池中多了 ACC_SYNCHRONIZED 标示符。


JVM 就是根据该标示符来实现方法的同步的:当方法调用时,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置。


如果设置了,执行线程将先获取 Monitor,获取成功之后才能执行方法体,方法执行完后再释放 Monitor。在方法执行期间,其他任何线程都无法再获得同一个 Monitor 对象。


其实本质上没有区别,只是方法的同步是一种隐式的方式来实现,无需通过字节码来完成。


运行结果解释


有了对 Synchronized 原理的认识,再来看上面的程序就可以迎刃而解了。


①代码段 2 结果


虽然 Method1 和 Method2 是不同的方法,但是这两个方法都进行了同步,并且是通过同一个对象去调用的。


所以调用之前都需要先去竞争同一个对象上的锁(Monitor),也就只能互斥的获取到锁,因此,Method1 和 Method2 只能顺序的执行。


②代码段 3 结果


虽然 Test 和 Test2 属于不同对象,但是 Test 和 Test2 属于同一个类的不同实例。


由于 Method1 和 Method2 都属于静态同步方法,所以调用的时候需要获取同一个类上 Monitor(每个类只对应一个 Class 对象),所以也只能顺序的执行。


③代码段 4 结果


对于代码块的同步,实质上需要获取 Synchronized 关键字后面括号中对象的 Monitor。


由于这段代码中括号的内容都是 This,而 Method1 和 Method2 又是通过同一的对象去调用的,所以进入同步块之前需要去竞争同一个对象上的锁,因此只能顺序执行同步块。


总结


Synchronized 是 Java 并发编程中最常用的用于保证线程安全的方式,其使用相对也比较简单。


但是如果能够深入了解其原理,对监视器锁等底层知识有所了解,一方面可以帮助我们正确的使用 Synchronized 关键字。


另一方面也能够帮助我们更好的理解并发编程机制,有助于我们在不同的情况下选择更优的并发策略来完成任务。对平时遇到的各种并发问题,也能够从容的应对。

目录
相关文章
|
10天前
|
人工智能 开发工具 iOS开发
Claude Code 新手完全上手指南:安装、国产模型配置与常用命令全解
Claude Code 是一款运行在终端环境中的 AI 编程助手,能够直接在命令行中完成代码生成、项目分析、文件修改、命令执行、Git 管理等开发全流程工作。它最大的特点是**任务驱动、终端原生、轻量高效、多模型兼容**,无需图形界面、不依赖 IDE 插件,能够深度融入开发者日常工作流。
3259 9
|
3天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
13天前
|
Shell API 开发工具
Claude Code 快速上手指南(新手友好版)
AI编程工具卷疯啦!Claude Code凭借任务驱动+终端原生的特性,成了开发者的效率搭子。本文从安装、登录、切换国产模型到常用命令,手把手带新手快速上手,全程避坑,30分钟独立用起来。
3317 23
|
7天前
|
人工智能 Linux BI
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
JeecgBoot AI专题研究 一键脚本:Claude Code + JeecgBoot Skills + DeepSeek 全平台接入 一行命令装好 Claude Code + JeecgBoot Skills + DeepSeek 接入,无需翻墙使用 Claude Code,支持 Wind
2334 4
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
|
26天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23597 15
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
13天前
|
人工智能 JSON BI
DeepSeek V4-Pro 接入 Claude Code 完全实战:体验、测试与关键避坑指南
Claude Code 作为当前主流的 AI 编程辅助工具,凭借强大的代码理解、工程执行与自动化能力深受开发者喜爱,但原生模型的使用成本相对较高。为了在保持能力的同时进一步降低开销,不少开发者开始寻找兼容度高、价格更友好的替代模型。DeepSeek V4 系列的发布带来了新的选择,该系列包含 V4-Pro 与 V4-Flash 两款模型,并提供了与 Anthropic 完全兼容的 API 接口,理论上只需简单修改配置,即可让 Claude Code 无缝切换为 DeepSeek 引擎。
2819 3
|
4天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全+三种模式+记忆体系+实战工作流完整手册
Claude Code 是当前最流行的终端级 AI 编程助手,能够直接在命令行中完成代码生成、项目理解、文件修改、命令执行、错误修复等全流程开发工作。它不依赖图形界面、不占用额外资源,却能深度理解项目结构,自动生成规范代码,大幅提升研发效率。
887 2
|
11天前
|
存储 Linux iOS开发
【2026最新】MarkText中文版Markdown编辑器使用图解(附安装包)
MarkText是一款免费开源、跨平台的Markdown编辑器,主打所见即所得实时预览,支持Windows/macOS/Linux。内置数学公式、流程图、代码高亮、多主题及PDF/HTML导出,是Typora的轻量免费替代首选。(239字)

热门文章

最新文章