当前位置:网站首页>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);
}
边栏推荐
- YYGH-BUG-04
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- Full link voltage measurement
- Heap (priority queue)
- HOW TO ADD P-VALUES TO GGPLOT FACETS
- WSL 2 will not be installed yet? It's enough to read this article
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- drools执行指定的规则
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- Those logs in MySQL
猜你喜欢
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
How to Add P-Values onto Horizontal GGPLOTS
PyTorch nn. Full analysis of RNN parameters
SVO2系列之深度滤波DepthFilter
CONDA common command summary
How does Premiere (PR) import the preset mogrt template?
How to Easily Create Barplots with Error Bars in R
使用Sqoop把ADS层数据导出到MySQL
PyTorch nn.RNN 参数全解析
随机推荐
【2022 ACTF-wp】
Leetcode209 subarray with the smallest length
Time format display
Leetcode209 长度最小的子数组
b格高且好看的代码片段分享图片生成
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Dynamic debugging of multi file program x32dbg
XSS labs master shooting range environment construction and 1-6 problem solving ideas
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
How to Visualize Missing Data in R using a Heatmap
GGPlot Examples Best Reference
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
Leetcode922 按奇偶排序数组 II
Filtre de profondeur de la série svo2
ES集群中节点与分片的区别
xss-labs-master靶场环境搭建与1-6关解题思路
[C language] Yang Hui triangle, customize the number of lines of the triangle
小程序链接生成
YYGH-BUG-05
Leetcode14 最长公共前缀