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

推荐订阅源

月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
IT之家
IT之家
Cyberwarzone
Cyberwarzone
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 叶小钗
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
Jina AI
Jina AI
T
Tor Project blog
V
Vulnerabilities – Threatpost
酷 壳 – CoolShell
酷 壳 – CoolShell
Spread Privacy
Spread Privacy
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Simon Willison's Weblog
Simon Willison's Weblog
Security Latest
Security Latest
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 司徒正美
V2EX - 技术
V2EX - 技术
I
Intezer
The Cloudflare Blog
Cisco Talos Blog
Cisco Talos Blog
SecWiki News
SecWiki News
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
Lohrmann on Cybersecurity
Scott Helme
Scott Helme
Google Online Security Blog
Google Online Security Blog
量子位
The Last Watchdog
The Last Watchdog
AI
AI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security Affairs
P
Palo Alto Networks Blog
S
Secure Thoughts
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs

博客园 - 时空穿越者

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 类)设计更优的线程安全方案。