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

推荐订阅源

WordPress大学
WordPress大学
月光博客
月光博客
T
Tailwind CSS Blog
Y
Y Combinator Blog
L
Lohrmann on Cybersecurity
Latest news
Latest news
H
Heimdal Security Blog
MongoDB | Blog
MongoDB | Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
阮一峰的网络日志
阮一峰的网络日志
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
小众软件
小众软件
Security Archives - TechRepublic
Security Archives - TechRepublic
The Cloudflare Blog
T
Threatpost
S
Secure Thoughts
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
爱范儿
爱范儿
SecWiki News
SecWiki News
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
D
DataBreaches.Net
S
Security @ Cisco Blogs
B
Blog RSS Feed
N
News and Events Feed by Topic
V
Visual Studio Blog
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 时空穿越者

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