JAVA面试-并发篇 09-LockSupport 和 waitnotify 的区别

LockSupport 与 wait/notify 核心区别详解

一、文章概述

wait/notify是Object类的原生线程通信方法,依赖synchronized同步锁;LockSupport是JUC提供的线程阻塞唤醒工具,是AQS的核心底层组件。本文通过对比表、流程图和完整可运行代码,清晰讲解两者的使用差异与核心特性。

二、核心区别对比

对比维度 wait/notify LockSupport
使用限制 必须在同步块中,否则抛异常 无锁限制,任意位置调用
执行顺序 必须先等待后唤醒 支持先唤醒后等待
唤醒精度 随机唤醒,无法指定线程 精准唤醒目标线程
锁资源 调用wait会自动释放锁 无锁,无需释放资源

三、执行流程

wait/notify 执行流程

#mermaid-svg-D3WhhelqCzWqQmeS{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-D3WhhelqCzWqQmeS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-D3WhhelqCzWqQmeS .error-icon{fill:#552222;}#mermaid-svg-D3WhhelqCzWqQmeS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-D3WhhelqCzWqQmeS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-D3WhhelqCzWqQmeS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-D3WhhelqCzWqQmeS .marker.cross{stroke:#333333;}#mermaid-svg-D3WhhelqCzWqQmeS svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-D3WhhelqCzWqQmeS p{margin:0;}#mermaid-svg-D3WhhelqCzWqQmeS .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-D3WhhelqCzWqQmeS .cluster-label text{fill:#333;}#mermaid-svg-D3WhhelqCzWqQmeS .cluster-label span{color:#333;}#mermaid-svg-D3WhhelqCzWqQmeS .cluster-label span p{background-color:transparent;}#mermaid-svg-D3WhhelqCzWqQmeS .label text,#mermaid-svg-D3WhhelqCzWqQmeS span{fill:#333;color:#333;}#mermaid-svg-D3WhhelqCzWqQmeS .node rect,#mermaid-svg-D3WhhelqCzWqQmeS .node circle,#mermaid-svg-D3WhhelqCzWqQmeS .node ellipse,#mermaid-svg-D3WhhelqCzWqQmeS .node polygon,#mermaid-svg-D3WhhelqCzWqQmeS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-D3WhhelqCzWqQmeS .rough-node .label text,#mermaid-svg-D3WhhelqCzWqQmeS .node .label text,#mermaid-svg-D3WhhelqCzWqQmeS .image-shape .label,#mermaid-svg-D3WhhelqCzWqQmeS .icon-shape .label{text-anchor:middle;}#mermaid-svg-D3WhhelqCzWqQmeS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-D3WhhelqCzWqQmeS .rough-node .label,#mermaid-svg-D3WhhelqCzWqQmeS .node .label,#mermaid-svg-D3WhhelqCzWqQmeS .image-shape .label,#mermaid-svg-D3WhhelqCzWqQmeS .icon-shape .label{text-align:center;}#mermaid-svg-D3WhhelqCzWqQmeS .node.clickable{cursor:pointer;}#mermaid-svg-D3WhhelqCzWqQmeS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-D3WhhelqCzWqQmeS .arrowheadPath{fill:#333333;}#mermaid-svg-D3WhhelqCzWqQmeS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-D3WhhelqCzWqQmeS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-D3WhhelqCzWqQmeS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-D3WhhelqCzWqQmeS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-D3WhhelqCzWqQmeS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-D3WhhelqCzWqQmeS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-D3WhhelqCzWqQmeS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-D3WhhelqCzWqQmeS .cluster text{fill:#333;}#mermaid-svg-D3WhhelqCzWqQmeS .cluster span{color:#333;}#mermaid-svg-D3WhhelqCzWqQmeS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-D3WhhelqCzWqQmeS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-D3WhhelqCzWqQmeS rect.text{fill:none;stroke-width:0;}#mermaid-svg-D3WhhelqCzWqQmeS .icon-shape,#mermaid-svg-D3WhhelqCzWqQmeS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-D3WhhelqCzWqQmeS .icon-shape p,#mermaid-svg-D3WhhelqCzWqQmeS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-D3WhhelqCzWqQmeS .icon-shape .label rect,#mermaid-svg-D3WhhelqCzWqQmeS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-D3WhhelqCzWqQmeS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-D3WhhelqCzWqQmeS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-D3WhhelqCzWqQmeS :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 获取synchronized锁
wait阻塞并释放锁
notify唤醒线程
重新竞争锁执行

LockSupport 执行流程

#mermaid-svg-V9nUbgZ0WJCRkaAI{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-V9nUbgZ0WJCRkaAI .error-icon{fill:#552222;}#mermaid-svg-V9nUbgZ0WJCRkaAI .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-V9nUbgZ0WJCRkaAI .marker{fill:#333333;stroke:#333333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .marker.cross{stroke:#333333;}#mermaid-svg-V9nUbgZ0WJCRkaAI svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-V9nUbgZ0WJCRkaAI p{margin:0;}#mermaid-svg-V9nUbgZ0WJCRkaAI .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .cluster-label text{fill:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .cluster-label span{color:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .cluster-label span p{background-color:transparent;}#mermaid-svg-V9nUbgZ0WJCRkaAI .label text,#mermaid-svg-V9nUbgZ0WJCRkaAI span{fill:#333;color:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .node rect,#mermaid-svg-V9nUbgZ0WJCRkaAI .node circle,#mermaid-svg-V9nUbgZ0WJCRkaAI .node ellipse,#mermaid-svg-V9nUbgZ0WJCRkaAI .node polygon,#mermaid-svg-V9nUbgZ0WJCRkaAI .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .rough-node .label text,#mermaid-svg-V9nUbgZ0WJCRkaAI .node .label text,#mermaid-svg-V9nUbgZ0WJCRkaAI .image-shape .label,#mermaid-svg-V9nUbgZ0WJCRkaAI .icon-shape .label{text-anchor:middle;}#mermaid-svg-V9nUbgZ0WJCRkaAI .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .rough-node .label,#mermaid-svg-V9nUbgZ0WJCRkaAI .node .label,#mermaid-svg-V9nUbgZ0WJCRkaAI .image-shape .label,#mermaid-svg-V9nUbgZ0WJCRkaAI .icon-shape .label{text-align:center;}#mermaid-svg-V9nUbgZ0WJCRkaAI .node.clickable{cursor:pointer;}#mermaid-svg-V9nUbgZ0WJCRkaAI .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .arrowheadPath{fill:#333333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-V9nUbgZ0WJCRkaAI .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-V9nUbgZ0WJCRkaAI .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-V9nUbgZ0WJCRkaAI .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-V9nUbgZ0WJCRkaAI .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .cluster text{fill:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI .cluster span{color:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-V9nUbgZ0WJCRkaAI .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-V9nUbgZ0WJCRkaAI rect.text{fill:none;stroke-width:0;}#mermaid-svg-V9nUbgZ0WJCRkaAI .icon-shape,#mermaid-svg-V9nUbgZ0WJCRkaAI .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-V9nUbgZ0WJCRkaAI .icon-shape p,#mermaid-svg-V9nUbgZ0WJCRkaAI .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-V9nUbgZ0WJCRkaAI .icon-shape .label rect,#mermaid-svg-V9nUbgZ0WJCRkaAI .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-V9nUbgZ0WJCRkaAI .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-V9nUbgZ0WJCRkaAI .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-V9nUbgZ0WJCRkaAI :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 调用park阻塞线程
unpark精准唤醒
线程恢复执行

四、完整代码示例

1. wait/notify 实现线程通信

java 复制代码
public class WaitNotifyDemo {
    private static final Object lock = new Object();
    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock) {
                try { lock.wait(); } // 阻塞并释放锁
                catch (InterruptedException e) {}
                System.out.println("线程被唤醒");
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) { lock.notify(); } // 必须加锁唤醒
        }).start();
    }
}

2. LockSupport 实现线程通信

java 复制代码
import java.util.concurrent.locks.LockSupport;
public class LockSupportDemo {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            LockSupport.park(); // 无锁阻塞
            System.out.println("线程被精准唤醒");
        });
        t.start();
        LockSupport.unpark(t); // 精准唤醒指定线程,无需加锁
    }
}

总结

wait/notify强依赖同步锁,使用繁琐且唤醒不精准,仅适用于基础同步场景;LockSupport无使用限制、支持预唤醒、精准操作线程,规避了传统API的缺陷,是Java高并发框架的首选线程协作工具。

相关推荐
怕浪猫5 小时前
第 1 章 FDE的崛起
面试
Chester_19995 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲7 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员7 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导7 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan7 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251497 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
2601_961901708 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..8 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
大衛說9 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习