摘要:解決SimpleDateFormat類在高并發(fā)場景下的線程安全問題可以有多種方式,這里,就列舉幾個(gè)常用的方式供參考。
本文分享自華為云社區(qū)《【高并發(fā)】更正SimpleDateFormat類線程不安全問題分析的錯(cuò)誤》,作者: 冰 河 。
解決SimpleDateFormat類在高并發(fā)場景下的線程安全問題可以有多種方式,這里,就列舉幾個(gè)常用的方式供參考,大家也可以在評(píng)論區(qū)給出更多的解決方案。
1.局部變量法
最簡單的一種方式就是將SimpleDateFormat類對(duì)象定義成局部變量,如下所示的代碼,將SimpleDateFormat類對(duì)象定義在parse(String)方法的上面,即可解決問題。
package io.binghe.concurrent.lab06; 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 binghe * @version 1.0.0 * @description 局部變量法解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest02 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); simpleDateFormat.parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
此時(shí)運(yùn)行修改后的程序,輸出結(jié)果如下所示。
所有線程格式化日期成功
至于在高并發(fā)場景下使用局部變量為何能解決線程的安全問題,會(huì)在【JVM專題】的JVM內(nèi)存模式相關(guān)內(nèi)容中深入剖析,這里不做過多的介紹了。
當(dāng)然,這種方式在高并發(fā)下會(huì)創(chuàng)建大量的SimpleDateFormat類對(duì)象,影響程序的性能,所以,這種方式在實(shí)際生產(chǎn)環(huán)境不太被推薦。
2.synchronized鎖方式
將SimpleDateFormat類對(duì)象定義成全局靜態(tài)變量,此時(shí)所有線程共享SimpleDateFormat類對(duì)象,此時(shí)在調(diào)用格式化時(shí)間的方法時(shí),對(duì)SimpleDateFormat對(duì)象進(jìn)行同步即可,代碼如下所示。
package io.binghe.concurrent.lab06; 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 binghe * @version 1.0.0 * @description 通過Synchronized鎖解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest03 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; //SimpleDateFormat對(duì)象 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { synchronized (simpleDateFormat){ simpleDateFormat.parse("2020-01-01"); } } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
此時(shí),解決問題的關(guān)鍵代碼如下所示。
synchronized (simpleDateFormat){ simpleDateFormat.parse("2020-01-01"); }
運(yùn)行程序,輸出結(jié)果如下所示。
所有線程格式化日期成功
需要注意的是,雖然這種方式能夠解決SimpleDateFormat類的線程安全問題,但是由于在程序的執(zhí)行過程中,為SimpleDateFormat類對(duì)象加上了synchronized鎖,導(dǎo)致同一時(shí)刻只能有一個(gè)線程執(zhí)行parse(String)方法。此時(shí),會(huì)影響程序的執(zhí)行性能,在要求高并發(fā)的生產(chǎn)環(huán)境下,此種方式也是不太推薦使用的。
3.Lock鎖方式
Lock鎖方式與synchronized鎖方式實(shí)現(xiàn)原理相同,都是在高并發(fā)下通過JVM的鎖機(jī)制來保證程序的線程安全。通過Lock鎖方式解決問題的代碼如下所示。
package io.binghe.concurrent.lab06; 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; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @author binghe * @version 1.0.0 * @description 通過Lock鎖解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest04 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; //SimpleDateFormat對(duì)象 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //Lock對(duì)象 private static Lock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { lock.lock(); simpleDateFormat.parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }finally { lock.unlock(); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
通過代碼可以得知,首先,定義了一個(gè)Lock類型的全局靜態(tài)變量作為加鎖和釋放鎖的句柄。然后在simpleDateFormat.parse(String)代碼之前通過lock.lock()加鎖。這里需要注意的一點(diǎn)是:為防止程序拋出異常而導(dǎo)致鎖不能被釋放,一定要將釋放鎖的操作放到finally代碼塊中,如下所示。
finally { lock.unlock(); }
運(yùn)行程序,輸出結(jié)果如下所示。
所有線程格式化日期成功
此種方式同樣會(huì)影響高并發(fā)場景下的性能,不太建議在高并發(fā)的生產(chǎn)環(huán)境使用。
4.ThreadLocal方式
使用ThreadLocal存儲(chǔ)每個(gè)線程擁有的SimpleDateFormat對(duì)象的副本,能夠有效的避免多線程造成的線程安全問題,使用ThreadLocal解決線程安全問題的代碼如下所示。
package io.binghe.concurrent.lab06; import java.text.DateFormat; 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 binghe * @version 1.0.0 * @description 通過ThreadLocal解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest05 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>(){ @Override protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); } }; public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { threadLocal.get().parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
通過代碼可以得知,將每個(gè)線程使用的SimpleDateFormat副本保存在ThreadLocal中,各個(gè)線程在使用時(shí)互不干擾,從而解決了線程安全問題。
運(yùn)行程序,輸出結(jié)果如下所示。
所有線程格式化日期成功
此種方式運(yùn)行效率比較高,推薦在高并發(fā)業(yè)務(wù)場景的生產(chǎn)環(huán)境使用。
另外,使用ThreadLocal也可以寫成如下形式的代碼,效果是一樣的。
package io.binghe.concurrent.lab06; import java.text.DateFormat; 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 binghe * @version 1.0.0 * @description 通過ThreadLocal解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest06 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>(); private static DateFormat getDateFormat(){ DateFormat dateFormat = threadLocal.get(); if(dateFormat == null){ dateFormat = new SimpleDateFormat("yyyy-MM-dd"); threadLocal.set(dateFormat); } return dateFormat; } public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { getDateFormat().parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
5.DateTimeFormatter方式
DateTimeFormatter是Java8提供的新的日期時(shí)間API中的類,DateTimeFormatter類是線程安全的,可以在高并發(fā)場景下直接使用DateTimeFormatter類來處理日期的格式化操作。代碼如下所示。
package io.binghe.concurrent.lab06; 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 binghe * @version 1.0.0 * @description 通過DateTimeFormatter類解決線程安全問題 */ public class SimpleDateFormatTest07 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { LocalDate.parse("2020-01-01", formatter); }catch (Exception e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
可以看到,DateTimeFormatter類是線程安全的,可以在高并發(fā)場景下直接使用DateTimeFormatter類來處理日期的格式化操作。
運(yùn)行程序,輸出結(jié)果如下所示。
所有線程格式化日期成功
使用DateTimeFormatter類來處理日期的格式化操作運(yùn)行效率比較高,推薦在高并發(fā)業(yè)務(wù)場景的生產(chǎn)環(huán)境使用。
6.joda-time方式
joda-time是第三方處理日期時(shí)間格式化的類庫,是線程安全的。如果使用joda-time來處理日期和時(shí)間的格式化,則需要引入第三方類庫。這里,以Maven為例,如下所示引入joda-time庫。
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.9</version> </dependency>
引入joda-time庫后,實(shí)現(xiàn)的程序代碼如下所示。
package io.binghe.concurrent.lab06; 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 binghe * @version 1.0.0 * @description 通過DateTimeFormatter類解決線程安全問題 */ public class SimpleDateFormatTest08 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時(shí)運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { DateTime.parse("2020-01-01", dateTimeFormatter).toDate(); }catch (Exception e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號(hào)量發(fā)生錯(cuò)誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }
這里,需要注意的是:DateTime類是org.joda.time包下的類,DateTimeFormat類和DateTimeFormatter類都是org.joda.time.format包下的類,如下所示。
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
運(yùn)行程序,輸出結(jié)果如下所示。
所有線程格式化日期成功
使用joda-time庫來處理日期的格式化操作運(yùn)行效率比較高,推薦在高并發(fā)業(yè)務(wù)場景的生產(chǎn)環(huán)境使用。
解決SimpleDateFormat類的線程安全問題的方案總結(jié)
綜上所示:在解決解決SimpleDateFormat類的線程安全問題的幾種方案中,局部變量法由于線程每次執(zhí)行格式化時(shí)間時(shí),都會(huì)創(chuàng)建SimpleDateFormat類的對(duì)象,這會(huì)導(dǎo)致創(chuàng)建大量的SimpleDateFormat對(duì)象,浪費(fèi)運(yùn)行空間和消耗服務(wù)器的性能,因?yàn)镴VM創(chuàng)建和銷毀對(duì)象是要耗費(fèi)性能的。所以,不推薦在高并發(fā)要求的生產(chǎn)環(huán)境使用。
synchronized鎖方式和Lock鎖方式在處理問題的本質(zhì)上是一致的,通過加鎖的方式,使同一時(shí)刻只能有一個(gè)線程執(zhí)行格式化日期和時(shí)間的操作。這種方式雖然減少了SimpleDateFormat對(duì)象的創(chuàng)建,但是由于同步鎖的存在,導(dǎo)致性能下降,所以,不推薦在高并發(fā)要求的生產(chǎn)環(huán)境使用。
ThreadLocal通過保存各個(gè)線程的SimpleDateFormat類對(duì)象的副本,使每個(gè)線程在運(yùn)行時(shí),各自使用自身綁定的SimpleDateFormat對(duì)象,互不干擾,執(zhí)行性能比較高,推薦在高并發(fā)的生產(chǎn)環(huán)境使用。
DateTimeFormatter是Java 8中提供的處理日期和時(shí)間的類,DateTimeFormatter類本身就是線程安全的,經(jīng)壓測,DateTimeFormatter類處理日期和時(shí)間的性能效果還不錯(cuò)(后文單獨(dú)寫一篇關(guān)于高并發(fā)下性能壓測的文章)。所以,推薦在高并發(fā)場景下的生產(chǎn)環(huán)境使用。
joda-time是第三方處理日期和時(shí)間的類庫,線程安全,性能經(jīng)過高并發(fā)的考驗(yàn),推薦在高并發(fā)場景下的生產(chǎn)環(huán)境使用。
?文章來源:http://www.zghlxwxcb.cn/news/detail-510937.html
點(diǎn)擊關(guān)注,第一時(shí)間了解華為云新鮮技術(shù)~文章來源地址http://www.zghlxwxcb.cn/news/detail-510937.html
到了這里,關(guān)于高并發(fā)場景下,6種解決SimpleDateFormat類的線程安全問題方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!