前因
Sonar有个指标是:使用SimpleDateFormat为线程不安全
按照建议修改为ThreadLocal<SimpleDateFormat>
ThreadLocal原理简单来说会拷贝一份对象副本,使得每个线程使用自己的对象副本,从而实现线程安全
验证
@Slf4j
public class ThreadLocalTest {
ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<>();
@Test
public void testThread() throws InterruptedException {
final int count = 10;
CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
new Thread(() -> {
log.info("Thread:{}, obj:{}", Thread.currentThread().getName(), getFormat().hashCode()); //使用hashCode
latch.countDown();
}).start();
}
latch.await();
}
private SimpleDateFormat getFormat() {
if (null == sdf.get()) {
sdf.set(new SimpleDateFormat());
}
return sdf.get();
}
}
11:17:35.284 [Thread-2] INFO ThreadLocalTest - Thread:Thread-2, obj:-1254875350
11:17:35.280 [Thread-1] INFO ThreadLocalTest - Thread:Thread-1, obj:-1254875350
11:17:35.280 [Thread-0] INFO ThreadLocalTest - Thread:Thread-0, obj:-1254875350
11:17:35.287 [Thread-4] INFO ThreadLocalTest - Thread:Thread-4, obj:-1254875350
11:17:35.287 [Thread-5] INFO ThreadLocalTest - Thread:Thread-5, obj:-1254875350
...省略部分
可以看出,打印的hashCode都是同一个值,天真的我以为ThreadLocal使用的是同一个对象,并不能保证线程安全 :(
原因
查看SimpleDateFormat源码,可以发现重写了hashCode
public int hashCode()
{
return this.pattern.hashCode(); //pattern就是yyyy那堆日期格式
}
由上知道,SimpleDateFormat的hashCode由字符串来提供,要是字符串内容为固定值,则hashCode也会固定
顺手写个Test来验证
@Test
public void testHash() throws InterruptedException {
final int count = 10;
CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
new Thread(() -> {
log.info("{}", "固定值".hashCode());
latch.countDown();
}).start();
}
latch.await();
}
//结果
11:18:14.925 [Thread-5] INFO ThreadLocalTest - 22145116
11:18:14.925 [Thread-8] INFO ThreadLocalTest - 22145116
11:18:14.926 [Thread-9] INFO ThreadLocalTest - 22145116
11:18:14.926 [Thread-1] INFO ThreadLocalTest - 22145116
11:18:14.926 [Thread-2] INFO ThreadLocalTest - 22145116
...省略
解决
回到开始的问题,那如何判断ThreadLocal用的是不同的对象
使用jdk自带System.identityHashCode(Object)
方法
public static int identityHashCode(Object x)
Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object’s class overrides hashCode(). The hash code for the null reference is zero.
根据定义可以知道,identityHashCode()
采用默认的hashCode实现,不管子类是否重写该方法
那我们顺手看下Object.hashCode()
(截取关键部分),感兴趣的可以全部官方API
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.
可以看到,合理范围内,不同的对象一定会返回不同的hash值
ps.只是验证结论,不考虑碰撞小概率事件
@Test
public void testThread() throws InterruptedException {
final int count = 10;
CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
new Thread(() -> {
log.info("Thread:{}, obj:{}", Thread.currentThread().getName(), getFormat().hashCode());
log.info("Thread:{}, mem:{}", Thread.currentThread().getName(), System.identityHashCode(getFormat())); // 增加identityHashCode用于比较验证
latch.countDown();
}).start();
}
latch.await();
}
11:18:50.145 [Thread-0] INFO ThreadLocalTest - Thread:Thread-0, obj:-1254875350 // 不同线程相同内容相同hash
11:18:50.149 [Thread-5] INFO ThreadLocalTest - Thread:Thread-5, obj:-1254875350 // 不同线程相同内容相同hash
11:18:50.153 [Thread-0] INFO ThreadLocalTest - Thread:Thread-0, mem:2006143742 // 不同线程不同的hash
11:18:50.149 [Thread-6] INFO ThreadLocalTest - Thread:Thread-6, obj:-1254875350
11:18:50.149 [Thread-7] INFO ThreadLocalTest - Thread:Thread-7, obj:-1254875350
11:18:50.145 [Thread-2] INFO ThreadLocalTest - Thread:Thread-2, obj:-1254875350
11:18:50.153 [Thread-6] INFO ThreadLocalTest - Thread:Thread-6, mem:806083499 // 不同线程不同的hash
11:18:50.146 [Thread-1] INFO ThreadLocalTest - Thread:Thread-1, obj:-1254875350
11:18:50.153 [Thread-1] INFO ThreadLocalTest - Thread:Thread-1, mem:1281545198
11:18:50.149 [Thread-8] INFO ThreadLocalTest - Thread:Thread-8, obj:-1254875350
...省略
总结
- ThreadLocal的确可以保证线程安全
- 对于判断对象是否为同一个,采用
System.identityHashCode(Object)
更为科学