惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 时空穿越者

java并发:深入解析 ThreadPoolExecutor.addWorker() 流水线技术解析:处理器重排序的硬件基础 java并发:synchronized 揭秘 java并发:管道流(Piped Streams)的应用场景 LangGraph:add_conditional_edges详解 Spring异步机制:@Async Spring BeanDefinition Spring Resource Spring之ApplicationContext Spring之BeanFactory:解析getBean()方法 Spring之IoC容器 Spring的整体架构 Spring Data JPA:解析CriteriaQuery Spring Data JPA:解析CriteriaBuilder Spring Data JPA:解析JpaSpecificationExecutor & Specification Spring Data JPA:解析SimpleJpaRepository java并发:线程池之Executors(ScheduledExecutorService篇) - 时空穿越者 java并发:线程池之饱和策略 java并发:线程池之ThreadPoolExecutor
java并发:再次认识一下Java中的锁 —— 类级别的锁是否存在?
时空穿越者 · 2026-02-08 · via 博客园 - 时空穿越者

背景

在《深入浅出Java多线程》一书中有下面这样一段描述:

image

那Java中有类级别的锁吗?

下面我们先来看一下这个示例:

public class Counter {
    private static int count = 0;

    // 类级别锁:锁定 Counter.class 对象
    public synchronized static void increment() {
        count++;
    }
}

简单解释:上述 Counter 类中定义了一个静态方法 increment,我们都知道在调用 increment 方法时,不需要创建 Counter 类型的对象实例;我们可以看到该方法由 synchronized 关键词修饰,那 increment 方法上面的锁是什么形式的呢?

(1)锁对象:Counter.class。

(2)作用范围:所有调用该方法的线程竞争同一把锁(跨实例同步)。

通过上面这个示例我们可以确定:在 Java 中,类级别的锁是存在的,其本质是通过锁定当前类的 Class 对象来实现全局同步。

—— 本质仍是对象锁的一种特殊形式

我们再来看另一个示例:

public void safeOperation() {
    synchronized (Counter.class) { // 显式锁定类对象
        // 临界区操作
    }
}

解释:

这个示例展示了可在非静态方法中实现类级别同步。

类锁 vs 对象锁

image

典型应用场景

全局配置更新

public class ConfigManager {
    private static Map<String, String> configMap = new HashMap<>();

    public synchronized static void updateConfig(String key, String value) {
        configMap.put(key, value); // 需全局互斥
    }
}

关键点:

避免多线程同时修改全局配置

单例模式的双重检查

public class Singleton {
    private static volatile Singleton instance;

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) { // 类锁保证原子性
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

关键点:

通过类锁确保单例创建的线程安全

注意事项

性能瓶颈
类锁的粒度较粗,高并发场景下易成为性能瓶颈(如所有操作串行化)。

死锁风险
跨模块协作时,若多个线程嵌套锁定不同类的 Class 对象,可能引发死锁:

// 线程1
synchronized (A.class) {
    synchronized (B.class) {...}
}

// 线程2
synchronized (B.class) {
    synchronized (A.class) {...}
}

总结

Java 的类级别锁通过锁定 Class 对象实现全局同步,但需谨慎使用以避免性能问题。在复杂系统中,推荐结合显式锁(如 ReentrantLock)或并发工具(如 Atomic 类)设计更优的线程安全方案。