当前位置:网站首页>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);
}
边栏推荐
- Maximum profit of jz63 shares
- 【2022 ACTF-wp】
- Codeforces 771 div2 B (no one FST, refers to himself)
- 基于Arduino和ESP8266的连接手机热点实验(成功)
- Uniapp uni list item @click, uniapp uni list item jump with parameters
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- 自然语言处理系列(三)——LSTM
- 小程序链接生成
- lombok常用注解
- Orb-slam2 data sharing and transmission between different threads
猜你喜欢

Seriation in R: How to Optimally Order Objects in a Data Matrice

Power Spectral Density Estimates Using FFT---MATLAB

堆(優先級隊列)

倍增 LCA(最近公共祖先)

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator

Mish shake the new successor of the deep learning relu activation function

conda常用命令汇总

GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R

Tas (file d'attente prioritaire)

drools中then部分的写法
随机推荐
(C语言)八进制转换十进制
输入一个三位的数字,输出它的个位数,十位数、百位数。
Leetcode209 长度最小的子数组
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
xss-labs-master靶场环境搭建与1-6关解题思路
刷题---二叉树--2
Leetcode739 daily temperature
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
[C language] convert decimal numbers to binary numbers
小程序链接生成
Applet link generation
Mish shake the new successor of the deep learning relu activation function
(C language) octal conversion decimal
How to Easily Create Barplots with Error Bars in R
YYGH-BUG-04
Jenkins user rights management
计算二叉树的最大路径和
CDH6之Sqoop添加数据库驱动
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
Easyexcel and Lombok annotations and commonly used swagger annotations