Java日期格式化线程安全性对比
2023-12-15 06:25:21
功能描述
100个线程同时使用一个SimpleDateFormat
或DateTimeFormatter
进行日期格式化,为了确保日期转换成字符串100个线程又各自创建了一个SimpleDateFormat
或DateTimeFormatter
实例进行格式化,最终得到正确的结果.使用AtomicInteger 统计日期格式化相同和不同的数量.
最终得出结论SimpleDateFormat
线程不安全,DateTimeFormatter
线程安全.
代码
package io.github.jast90.java.api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author zhangzhiwen
* @Description:
* @date 2021/12/30 10:12
*/
public class DateFormatConcurrencyTest {
public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
simpleDateFormatInConcurrency();
}
System.out.println();
for (int i = 0; i < 10; i++) {
dateTimeFormatterInConcurrency();
}
}
public static void simpleDateFormatInConcurrency() {
System.out.println("-----------simpleDateFormatInConcurrency start-----------");
AtomicInteger sameCount = new AtomicInteger(0);
AtomicInteger notSameCount = new AtomicInteger(0);
ExecutorService executorService = new ThreadPoolExecutor(100, 100, 0L,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setUncaughtExceptionHandler((t, e) -> {
notSameCount.incrementAndGet();
});
return thread;
}
});
for (int i = 0; i < 100; i++) {
executorService.execute(() -> {
LocalDate now = LocalDate.now();
String dateStr = now.toString();
Date parse = null;
try {
parse = simpleDateFormat.parse(now.toString());
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String parseStr = mySimpleDateFormat.format(parse);
if (!Objects.equals(dateStr, parseStr)) {
notSameCount.addAndGet(1);
} else {
sameCount.addAndGet(1);
}
});
}
executorService.shutdown();
while (true) {
if (executorService.isTerminated()) {
System.out.println(String.format("总结:sameCount:%s notSameCount:%s", sameCount.get(), notSameCount.get()));
break;
}
}
System.out.println("-----------simpleDateFormatInConcurrency end-----------");
}
public static void dateTimeFormatterInConcurrency() {
System.out.println("-----------dateTimeFormatterInConcurrency start-----------");
ExecutorService executorService = new ThreadPoolExecutor(100, 100, 0L,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
AtomicInteger sameCount = new AtomicInteger(0);
AtomicInteger notSameCount = new AtomicInteger(0);
for (int i = 0; i < 100; i++) {
executorService.execute(() -> {
LocalDate now = LocalDate.now();
String dateStr = now.toString();
TemporalAccessor parse = dateTimeFormatter.parse(now.toString());
DateTimeFormatter myDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String parseStr = myDateTimeFormatter.format(parse);
if (Objects.equals(dateStr, parseStr)) {
sameCount.addAndGet(1);
} else {
notSameCount.addAndGet(1);
}
});
}
executorService.shutdown();
while (true) {
if (executorService.isTerminated()) {
System.out.println(String.format("总结:sameCount:%s notSameCount:%s", sameCount.get(), notSameCount.get()));
break;
}
}
System.out.println("-----------dateTimeFormatterInConcurrency end-----------");
}
}
输出
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:66 notSameCount:34
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:84 notSameCount:16
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:91 notSameCount:9
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:93 notSameCount:7
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:95 notSameCount:5
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:98 notSameCount:2
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:98 notSameCount:2
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:89 notSameCount:11
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:90 notSameCount:10
-----------simpleDateFormatInConcurrency end-----------
-----------simpleDateFormatInConcurrency start-----------
总结:sameCount:91 notSameCount:9
-----------simpleDateFormatInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
-----------dateTimeFormatterInConcurrency start-----------
总结:sameCount:100 notSameCount:0
-----------dateTimeFormatterInConcurrency end-----------
文章来源:https://blog.csdn.net/jastzhang/article/details/122231652
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!