当前位置:网站首页>Fastdateformat why thread safe
Fastdateformat why thread safe
2022-07-02 12:09:00 【A cat wandering in the middle of the night】
Chapter one FastDateFormat Why thread safe
List of articles
Link to the original text https://zhhll.icu/2021/ Third party tools /1.FastDateFormat Why thread safe /
FastDateFormat Why thread safe
SimpleDateFormat The thread of is not safe
Everybody knows SimpleDateFormat It's not thread safe
protected Calendar calendar;
SimpleDateFormat Medium calendar Is a member variable , Multiple threads of the same instance will share this calendar object
When formatting, it may be because the first thread has not finished formatting , The second thread has modified the time
private StringBuffer format(Date date, StringBuffer toAppendTo,
FieldDelegate delegate) {
// If the first thread has not been formatted as a string after setting the time , At this time, the second thread overwrites the time , There will be thread safety issues
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 How to deal with it
that FastDateFormat Why is thread safe ? First FastDateFormat There is a cache , When instantiating, it is through cache Cache to get the instance
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);
}
Format will be formatted 、 Time zone and internationalization as a key There is cInstanceCache in ,cInstanceCache It's a ConcurrentHashMap, Equivalent to the same format 、 Time zone and internationalization will use the same FastDateFormat example
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;
}
}
When formatting Calendar It uses local variables inside the method , There will be no thread safety problems
public String format(final Date date) {
final Calendar c = newCalendar();
c.setTime(date);
return applyRulesToString(c);
}
边栏推荐
- Leetcode209 长度最小的子数组
- CDA data analysis -- Introduction and use of aarrr growth model
- Natural language processing series (II) -- building character level language model using RNN
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- Depth filter of SvO2 series
- CDA数据分析——Excel数据处理的常见知识点归纳
- 深入理解P-R曲线、ROC与AUC
- SSH automatically disconnects (pretends to be dead) after a period of no operation
- Time format display
猜你喜欢
Mysql database foundation
GGPlot Examples Best Reference
小程序链接生成
【C语言】十进制数转换成二进制数
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
SVO2系列之深度濾波DepthFilter
Sort---
YYGH-BUG-05
MSI announced that its motherboard products will cancel all paper accessories
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
随机推荐
Pytorch builds LSTM to realize clothing classification (fashionmnist)
PyTorch nn.RNN 参数全解析
【C语言】十进制数转换成二进制数
mysql索引和事务
自然语言处理系列(一)——RNN基础
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
YYGH-BUG-05
CDH6之Sqoop添加数据库驱动
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
drools执行完某个规则后终止别的规则执行
drools决策表的简单使用
Jenkins用户权限管理
PyTorch搭建LSTM实现服装分类(FashionMNIST)
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
史上最易懂的f-string教程,收藏這一篇就够了
Deep understanding of NN in pytorch Embedding
Jenkins voucher management
How to Visualize Missing Data in R using a Heatmap
输入一个三位的数字,输出它的个位数,十位数、百位数。