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

推荐订阅源

Google Online Security Blog
Google Online Security Blog
博客园_首页
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
博客园 - 叶小钗
Jina AI
Jina AI
罗磊的独立博客
Simon Willison's Weblog
Simon Willison's Weblog
Scott Helme
Scott Helme
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
Know Your Adversary
Know Your Adversary
博客园 - Franky
AWS News Blog
AWS News Blog
S
Schneier on Security
K
Kaspersky official blog
I
Intezer
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
L
LINUX DO - 热门话题
GbyAI
GbyAI
月光博客
月光博客
C
Cisco Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
T
Tor Project blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security
S
Security Affairs
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
T
Troy Hunt's Blog

博客园 - 时空穿越者

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