当前位置:网站首页>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);
}
边栏推荐
- (C language) octal conversion decimal
- Map和Set
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- Jenkins用户权限管理
- FastDateFormat为什么线程安全
- 计算二叉树的最大路径和
- Applet link generation
- Dynamic debugging of multi file program x32dbg
- 还不会安装WSL 2?看这一篇文章就够了
- Mish-撼动深度学习ReLU激活函数的新继任者
猜你喜欢

(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?

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

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开

Jenkins用户权限管理

WSL 2 will not be installed yet? It's enough to read this article

How does Premiere (PR) import the preset mogrt template?

Deep understanding of NN in pytorch Embedding

刷题---二叉树--2

How to Visualize Missing Data in R using a Heatmap
随机推荐
Lombok common annotations
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
[C language] Yang Hui triangle, customize the number of lines of the triangle
Applet link generation
Read the Flink source code and join Alibaba cloud Flink group..
XSS labs master shooting range environment construction and 1-6 problem solving ideas
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
post请求体内容无法重复获取
ThreadLocal的简单理解
Yygh-9-make an appointment to place an order
xss-labs-master靶场环境搭建与1-6关解题思路
ORB-SLAM2不同线程间的数据共享与传递
b格高且好看的代码片段分享图片生成
MSI announced that its motherboard products will cancel all paper accessories
Maximum profit of jz63 shares
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
还不会安装WSL 2?看这一篇文章就够了