前言
在阿里Java開發規約中,有強制性的提到SimpleDateFormat 是線程不安全的類 ,在使用的時候應當注意線程安全問題,如下:
其實之前已經介紹過使用JDK1.8的DateTimeFormatter 和LocalDateTime來處理時間了,還在用SimpleDateFormat?Java8都發布N年了,轉LocalDateTime吧。今天,就來說說SimpleDateFormat的線程安全問題。
SimpleDateFormat是非線程安全的
時間處理,基本所有項目上都是需要使用到的,往往很多初學者會把SimpleDateFormat定義為static類型,然后在進行時間轉化的時候沒有做加鎖處理。如下:
public class Main {
private final static SimpleDateFormat SDFT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws ParseException {
System.out.println(SDFT.parse("2019-05-29 12:12:12"));
}
}
當然,本代碼直接運行是沒有問題的。但是,當此SDFT實例應用到多線程環境下的時候,就會出現致命的問題。假設有如下代碼:
public class Main {
private static SimpleDateFormat SDFT = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
for (int i = 1; i < 31; i++) {
int ii = i;
new Thread(() -> {
Date date = null;
try {
String s = "2019-05-" + ii;
date = SDFT.parse(s);
System.out.println("" + ii + ":" + date.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}).start();
}
}
}
此代碼的意思是創建30個線程,去轉化不同的時間字符串,然后做打印輸出,運行結果:
(運行此代碼也有可能出現由線程安全問題引起的異常)
根據“預期結果”,兩邊的數字應該是相等的,為何這里輸出不相等呢?通過DateFormat源碼可以查看:
因為SimpleDateFormat定義為了共享的,所以其類里的屬性calendar也是多個線程共享的,這就造成了線程安全問題。
解決方案
方案一:加鎖處理
如本文例子,可以通過加鎖來保證線程安全:
public class Main {
private static SimpleDateFormat SDFT = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
for (int i = 1; i < 31; i++) {
int ii = i;
new Thread(() -> {
Date date = null;
try {
String s = "2019-05-" + ii;
synchronized (Main.class) {
date = SDFT.parse(s);
}
System.out.println("" + ii + ":" + date.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}).start();
}
}
}
輸出:
4:4
3:3
1:1
2:2
29:29
28:28
27:27
26:26
30:30
25:25
23:23
21:21
20:20
22:22
18:18
24:24
19:19
17:17
16:16
14:14
15:15
12:12
13:13
10:10
11:11
9:9
7:7
6:6
5:5
8:8
方案二:每次都創建SimpleDateFormat實例
代碼改造如下:
public static void main(String[] args) {
for (int i = 1; i < 31; i++) {
int ii = i;
new Thread(() -> {
Date date = null;
try {
String s = "2019-05-" + ii;
date = new SimpleDateFormat("yyyy-MM-dd").parse(s);
System.out.println("" + ii + ":" + date.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}).start();
}
}
每次使用SimpleDateFormat的時候,都去創建一個SimpleDateFormat實例,保證SimpleDateFormat實例不被共享。
方案三:使用LocalThread
這是阿里Java規約里提到的解決方法之一,之所以可以使用LocalThread來解決此問題,代碼改造如下:
public class Main {
private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public static void main(String[] args) {
for (int i = 1; i < 31; i++) {
int ii = i;
new Thread(() -> {
Date date = null;
try {
String s = "2019-05-" + ii;
date = threadLocal.get().parse(s);
System.out.println("" + ii + ":" + date.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}).start();
}
}
}
運行結果如下:
22:22
2:2
24:24
15:15
17:17
16:16
29:29
9:9
30:30
3:3
4:4
5:5
12:12
8:8
20:20
26:26
21:21
28:28
19:19
27:27
18:18
1:1
14:14
25:25
11:11
13:13
7:7
6:6
23:23
10:10
解決方法四:使用JDK1.8提供的DateTimeFormatter來處理時間,這里就不贅述了,可以參考我之前的文章。