当前位置:网站首页>FastDateFormat为什么线程安全
FastDateFormat为什么线程安全
2022-07-02 09:42:00 【徘徊在深夜中的猫】
原文链接 https://zhhll.icu/2021/第三方工具/1.FastDateFormat为什么线程安全/
FastDateFormat为什么线程安全
SimpleDateFormat的线程不安全
大家都知道SimpleDateFormat是线程不安全的
protected Calendar calendar;
SimpleDateFormat中的calendar是成员变量,同实例多个线程下会共享该calendar对象
而在进行格式化的时候可能会由于第一个线程还没有格式化完成,而第二个线程已经将时间修改了的情况
private StringBuffer format(Date date, StringBuffer toAppendTo,
FieldDelegate delegate) {
// 如果第一个线程设置了时间之后还没有格式化为字符串,此时第二个线程将时间覆盖掉,就会出现线程安全问题
calendar.setTime(date);
boolean useDateFormatSymbols = useDateFormatSymbols();
for (int i = 0; i < compiledPattern.length; ) {
int tag = compiledPattern[i] >>> 8;
int count = compiledPattern[i++] & 0xff;
if (count == 255) {
count = compiledPattern[i++] << 16;
count |= compiledPattern[i++];
}
switch (tag) {
case TAG_QUOTE_ASCII_CHAR:
toAppendTo.append((char)count);
break;
case TAG_QUOTE_CHARS:
toAppendTo.append(compiledPattern, i, count);
i += count;
break;
default:
subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
break;
}
}
return toAppendTo;
}
FastDateFormat如何处理的呢
那么FastDateFormat为什么是线程安全的呢?首先FastDateFormat是有一个缓存的,在进行实例化的时候是通过cache缓存来获取实例的
private static final FormatCache<FastDateFormat> cache= new FormatCache<FastDateFormat>() {
@Override
protected FastDateFormat createInstance(final String pattern, final TimeZone timeZone, final Locale locale) {
return new FastDateFormat(pattern, timeZone, locale);
}
};
public static FastDateFormat getInstance(final String pattern) {
return cache.getInstance(pattern, null, null);
}
将格式化格式、时区和国际化作为一个key存在了cInstanceCache中,cInstanceCache是一个ConcurrentHashMap,相当于相同的格式化格式、时区和国际化会使用同一个FastDateFormat实例
final MultipartKey key = new MultipartKey(pattern, timeZone, locale);
F format = cInstanceCache.get(key);
if (format == null) {
format = createInstance(pattern, timeZone, locale);
final F previousValue= cInstanceCache.putIfAbsent(key, format);
if (previousValue != null) {
// another thread snuck in and did the same work
// we should return the instance that is in ConcurrentMap
format= previousValue;
}
}
而在进行格式化的时候Calendar使用的是方法内部的局部变量,是不会出现线程安全问题的
public String format(final Date date) {
final Calendar c = newCalendar();
c.setTime(date);
return applyRulesToString(c);
}
边栏推荐
- The selected cells in Excel form have the selection effect of cross shading
- 机械臂速成小指南(七):机械臂位姿的描述方法
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- ES集群中节点与分片的区别
- 输入一个三位的数字,输出它的个位数,十位数、百位数。
- [geek challenge 2019] upload
- Log4j2
- Filtre de profondeur de la série svo2
- 进入前六!博云在中国云管理软件市场销量排行持续上升
- Depth filter of SvO2 series
猜你喜欢

Power Spectral Density Estimates Using FFT---MATLAB

Natural language processing series (II) -- building character level language model using RNN

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

SVO2系列之深度濾波DepthFilter

jenkins 凭证管理

How to Add P-Values onto Horizontal GGPLOTS

How to Visualize Missing Data in R using a Heatmap

Cluster Analysis in R Simplified and Enhanced
![[visual studio 2019] create and import cmake project](/img/51/6c2575030c5103aee6c02bec8d5e77.jpg)
[visual studio 2019] create and import cmake project

pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
随机推荐
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
Dynamic debugging of multi file program x32dbg
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
xss-labs-master靶场环境搭建与1-6关解题思路
Fabric. JS 3 APIs to set canvas width and height
浅谈sklearn中的数据预处理
Le tutoriel F - String le plus facile à comprendre de l'histoire.
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
深入理解P-R曲线、ROC与AUC
Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
小程序链接生成
This article takes you to understand the operation of vim
Develop scalable contracts based on hardhat and openzeppelin (I)
YYGH-BUG-05
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
测试左移和右移
Leetcode topic [array] -540- single element in an ordered array
SVO2系列之深度滤波DepthFilter
jenkins 凭证管理
The most understandable f-string tutorial in history, collecting this one is enough