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

推荐订阅源

博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
小众软件
小众软件
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
A
About on SuperTechFans
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
博客园_首页
T
Troy Hunt's Blog
I
InfoQ
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
博客园 - 司徒正美
Cloudbric
Cloudbric
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
D
Docker
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
博客园 - 聂微东
S
Security Affairs
Security Archives - TechRepublic
Security Archives - TechRepublic
WordPress大学
WordPress大学
N
News and Events Feed by Topic
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Heimdal Security Blog
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
A
Arctic Wolf
J
Java Code Geeks
F
Full Disclosure
L
Lohrmann on Cybersecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Project Zero
Project Zero
GbyAI
GbyAI
B
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 类)设计更优的线程安全方案。