住宅梦物语DX
52.25M · 2026-02-05
在高并发服务的性能排查中,我们通过线程 dump 发现了大量线程阻塞在同一把锁上。本文将详细分析问题根因,并介绍我的优化方案。
在生产环境进行 thread dump 时,发现多个工作线程(20+)处于 BLOCKED 状态,等待同一个 Class 对象锁:
at java.util.TimeZone.getTimeZone(TimeZone.java:549)
- waiting on java.lang.Class@37b99a71
at org.joda.time.DateTimeZone.toTimeZone(DateTimeZone.java:1250)
at org.joda.time.base.AbstractDateTime.toGregorianCalendar(AbstractDateTime.java:296)
at xxx.common.DateTimeUtils.toCalendar(DateTimeUtils.java:469)
at xxx.business.RefundEndorseBusiness.doSetRefundEndorseFee(...)
...
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
关键信息:
java.util.TimeZone.getTimeZonejava.lang.Class@37b99a71(类锁)DateTimeZone.toTimeZone() 方法TimeZone.getTimeZone(String ID) 方法内部实现使用了 synchronized 关键字:
public static synchronized TimeZone getTimeZone(String ID) {
// 从缓存或文件加载时区信息
...
}
这意味着所有调用该方法的线程都需要竞争同一把类锁。
分析 Joda-Time 源码,DateTime.toGregorianCalendar() 的实现如下:
// org.joda.time.base.AbstractDateTime
public GregorianCalendar toGregorianCalendar() {
DateTimeZone zone = getZone();
GregorianCalendar cal = new GregorianCalendar(zone.toTimeZone());
cal.setTime(toDate());
return cal;
}
每次调用都会执行 zone.toTimeZone(),最终触发 TimeZone.getTimeZone()。
┌─────────────────────────────────────────────────────────────────┐
│ 高并发请求 │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Thread-1 Thread-2 Thread-3 ... Thread-N │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ toCalendar() toCalendar() toCalendar() toCalendar() │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ toGregorianCalendar() ──────────────────────────────────── │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ toTimeZone() ───────────────────────────────────────────── │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ TimeZone.getTimeZone() - synchronized 类锁 │ │
│ │ │ │
│ │ Thread-1: 获取锁,执行中... │ │
│ │ Thread-2: BLOCKED (waiting on java.lang.Class) │ │
│ │ Thread-3: BLOCKED (waiting on java.lang.Class) │ │
│ │ ... │ │
│ │ Thread-N: BLOCKED (waiting on java.lang.Class) │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
核心问题:
toTimeZone() 调用TimeZone.getTimeZone() 是同步方法(JDK 8)Asia/Shanghai、UTC 等)public static Calendar toCalendar(DateTime dateTime) {
Calendar result;
if (dateTime == null || DateTimeUtils.isLogicMin(dateTime)) {
result = new GregorianCalendar(1, 0, 1, 0, 0, 0);
result.setTimeZone(TimeZone.getTimeZone("UTC"));
return result;
}
if (DateTimeUtils.isLogicMax(dateTime)) {
result = new GregorianCalendar(9999, Calendar.DECEMBER, 31, 0, 0, 0);
result.setTimeZone(TimeZone.getTimeZone("UTC"));
return result;
}
// 问题所在:每次都调用 toGregorianCalendar()
result = dateTime.toGregorianCalendar();
return result;
}
由于底层API在项目中频繁使用,全部改动风险比较大,我也不想因为出错而背锅,所以考虑消除锁竞争的方案。
既然时区数量有限,我们可以缓存 DateTimeZone 到 TimeZone 的映射,避免重复调用 toTimeZone()。
| 方案 | 优点 | 缺点 |
|---|---|---|
HashMap + synchronized | 实现简单 | 锁粒度粗,与原问题类似 |
ConcurrentHashMap.computeIfAbsent | JDK 原生,无依赖 | 首次加载时仍有锁竞争;长 key 可能 hash 冲突 |
| Caffeine | 高性能、自动驱逐、统计监控 | 引入额外依赖 |
| Guava Cache | 功能完善 | 性能略逊于 Caffeine |
选择 Caffeine 的原因:
/**
* 缓存开关,驱逐时降级为直接调用
*/
static volatile boolean zoneCacheEnable = true;
/**
* DateTimeZone -> TimeZone 缓存
* 生产环境时区种类有限(通常 < 5 种),设置 maximumSize = 24 足够
*/
static LoadingCache<DateTimeZone, TimeZone> zoneCache = Caffeine.newBuilder()
.maximumSize(24)
.evictionListener((k, v, c) -> {
// 正常情况不应该触发驱逐,如果触发说明时区种类异常多
LOGGER.error("zone cache evicted k={}, v={}, cause={}", k, v, c);
zoneCacheEnable = false;
})
.build(DateTimeZone::toTimeZone);
/**
* 从缓存获取 TimeZone,带 fallback
*/
static TimeZone getTimeZoneFromCache(DateTimeZone zone) {
return zoneCacheEnable ? zoneCache.get(zone) : zone.toTimeZone();
}
/**
* 优化后的 toCalendar 方法
*/
public static Calendar toCalendar(DateTime dateTime) {
Calendar result;
if (dateTime == null || DateTimeUtils.isLogicMin(dateTime)) {
result = new GregorianCalendar(1, 0, 1, 0, 0, 0);
result.setTimeZone(TimeZone.getTimeZone("UTC"));
return result;
}
if (DateTimeUtils.isLogicMax(dateTime)) {
result = new GregorianCalendar(9999, Calendar.DECEMBER, 31, 0, 0, 0);
result.setTimeZone(TimeZone.getTimeZone("UTC"));
return result;
}
// 优化:从缓存获取 TimeZone,避免锁竞争
DateTimeZone zone = dateTime.getZone();
TimeZone timeZone = getTimeZoneFromCache(zone);
GregorianCalendar cal = new GregorianCalendar(timeZone);
cal.setTime(dateTime.toDate());
return cal;
}
优化前:
┌─────────────────────────────────────────────────────────┐
│ N 个线程同时调用 toCalendar() │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ TimeZone.getTimeZone() - synchronized 类锁 │ │
│ │ 所有线程串行等待 │ │
│ └─────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
优化后:
┌─────────────────────────────────────────────────────────┐
│ N 个线程同时调用 toCalendar() │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Caffeine Cache - 无锁读取 │ │
│ │ 所有线程并行获取缓存的 TimeZone │ │
│ └─────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
Asia/Shanghai、UTC、Asia/Hong_Kong 等少数几个时区.evictionListener((k, v, c) -> {
LOGGER.error("zone cache evicted k={}, v={}, cause={}", k, v, c);
zoneCacheEnable = false;
})
static volatile boolean zoneCacheEnable = true;
通过引入 Caffeine 缓存,我们成功消除了 TimeZone.getTimeZone() 的锁竞争问题。这个优化的核心思想是:对于数量有限、创建成本高的对象,使用缓存来换取并发性能。
不要使用过时的API。