@[TOC]
一、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();
}
}
线程安全,正常输出