Focus on micro-channel public number beauty of technology architecture , technology and learning materials to learn more.
Article Directory
一、java.text.SimpleDateFormat
The instance object of java.text.SimpleDateFormat will throw a conversion exception when it is shared and used by multiple threads. The correct way to use it should be to use stack closure and use it as a local variable in the method instead of a global variable. Every time the method is called It is time to create a SimpleDateFormat instance object, so that the use of stack closure will not cause concurrency problems.
Thread is not safe, throw exception writing:
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();
}
}
Thread safe writing:
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();
}
}
Two, joda-time
The DateTimeFormatter class of the third-party library joda-time is thread-safe.
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 provides many immutable and thread-safe classes, such as java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime and java.time.format.DateTimeFormatter, etc.
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();
}
}
Thread safe, normal output