浅谈 SimpleDateFormat,第三方库joda-time,JDK8提供时间类 之性能和线程安全
发布日期:2021-06-30 12:37:18
浏览次数:2
分类:技术文章
本文共 6919 字,大约阅读时间需要 23 分钟。
文章目录
一、java.text.SimpleDateFormat
java.text.SimpleDateFormat 的实例对象在多线程共享使用的时候会抛出转换异常,正确的使用方法应该是采用堆栈封闭,将其作为方法内的局部变量而不是全局变量,在每次调用方法的时候才去创建一个SimpleDateFormat实例对象,这样利用堆栈封闭就不会出现并发问题。
线程不安全,抛出异常写法:
package com.nobody.part02;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;/** * @author Mr.nobody * @Description * @date 2020/9/17 */public class SimpleDateFormatDemo { // 请求总数 private static int CLIENT_COUNT = 10000; // 并发线程数 private static int THREAD_COUNT = 500; // 全局变量,多线程访问会线程不安全,抛异常 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定线程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); simpleDateFormat.parse("20200917"); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有请求执行完 countDownLatch.await(); // 关闭线程池 executorService.shutdown(); }}
线程安全写法:
package com.nobody.part02;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;/** * @author Mr.nobody * @Description * @date 2020/9/17 */public class SimpleDateFormatDemo { // 请求总数 private static int CLIENT_COUNT = 10000; // 并发线程数 private static int THREAD_COUNT = 500; // 全局变量,多线程访问会线程不安全,抛异常 //private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定线程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); // 局部变量,线程安全 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); simpleDateFormat.parse("20200917"); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有请求执行完 countDownLatch.await(); // 关闭线程池 executorService.shutdown(); }}
二、joda-time
第三方库 joda-time 的 DateTimeFormatter 类是线程安全的。
package com.nobody.part02;import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;/** * @author Mr.nobody * @Description * @date 2020/9/17 */public class JodaTimeDemo { // 请求总数 private static int CLIENT_COUNT = 10000; // 并发线程数 private static int THREAD_COUNT = 500; // 全局变量,线程安全 private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定线程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); DateTime.parse("20200917", dateTimeFormatter).toDate(); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有请求执行完 countDownLatch.await(); // 关闭线程池 executorService.shutdown(); }}
三、java.time.format.DateTimeFormatter
JDK8提供了许多不可变且线程安全的类,例如 java.time.LocalDate 、java.time.LocalTime,java.time.LocalDateTime 以及java.time.format.DateTimeFormatter等等。
package com.nobody.part02;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;/** * @author Mr.nobody * @Description * @date 2020/9/17 */public class JDK8TimeDemo { // 请求总数 private static int CLIENT_COUNT = 10000; // 并发线程数 private static int THREAD_COUNT = 500; // 全局变量,JDK8时间类,线程安全 private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定线程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); System.out.println(LocalDate.parse("20200917", dateTimeFormatter)); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有请求执行完 countDownLatch.await(); // 关闭线程池 executorService.shutdown(); }}
线程安全,正常输出
转载地址:https://javalib.blog.csdn.net/article/details/108655440 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月07日 01时05分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SpringBoot内置Tomcat配置参数
2019-04-30
局部加权回归
2019-04-30
ubuntu 快捷键
2019-04-30
linux 根目录下文件夹分析
2019-04-30
linux 查看分区和文件大小
2019-04-30
Not using PCAP_FRAMES 解释(snort中)
2019-04-30
技术转管理?这些“坑”你要绕道走
2019-04-30
领域驱动设计(DDD)前夜:面向对象思想
2019-04-30
Camera驱动调试小记
2019-04-30
对于中断函数返回值的分析
2019-04-30
四线触摸屏原理
2019-04-30
C/C++如何返回一个数组/指针
2019-04-30
腾讯AI语音识别API踩坑记录
2019-04-30
YbtOJ——递推算法【例题4】传球游戏
2019-04-30
安装openrave 0.9的各种依赖包
2019-05-01
trajopt代码使用
2019-05-01
kpm代码使用细节
2019-05-01
redis
2019-05-01
@FeignClient注解的重复名称解决
2019-05-01
java.net.BindException: 无法指定被请求的地址
2019-05-01