浅谈 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JDBC 连接 MySQL 报错 Unknown system variable ‘query_cache_size‘
下一篇:浅谈 String StringBuilder StringBuffer 之性能和线程安全

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月07日 01时05分36秒