生活随笔
收集整理的這篇文章主要介紹了
SimpleDateFormat非线程安全
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
類SimpleDateFormat主要負責日期的格式化和轉換,但是在多線程的環境中,使用此類容易造成數據轉換和處理不正確,因為SimpleDateFormat類并不是線程安全的。
package com.test.test4;import java.text.SimpleDateFormat;/*** SimpleDateFormat非線程安全* Created by admin on 2018/12/18.*/
public class Test {public static void main(String[] args) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String[]dataStringArray = new String[]{"2000-01-01","2000-01-02","2000-01-03","2000-01-04","2000-01-05"};MyThread[] threadArray = new MyThread[5];for (int i=0;i<5;i++) {threadArray[i] = new MyThread(sdf,dataStringArray[i]);}for (int i=0;i<5;i++) {threadArray[i].start();}}}package com.test.test4;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** Created by admin on 2018/12/18.*/
public class MyThread extends Thread {private SimpleDateFormat sdf;private String dateString;public MyThread(SimpleDateFormat sdf,String dateString){super();this.sdf = sdf;this.dateString = dateString;}@Overridepublic void run(){try {Date dateRef = sdf.parse(dateString);String newDateString = sdf.format(dateRef).toString();if (!newDateString.equals(dateString)) {System.out.println("ThreadName="+this.getName()+"報錯了 日期字符串:"+dateString+"轉換成的日期為"+newDateString);}} catch (ParseException e) {e.printStackTrace();}}}
ThreadName=Thread-4報錯了 日期字符串:2000-01-05轉換成的日期為0001-01-02
ThreadName=Thread-2報錯了 日期字符串:2000-01-03轉換成的日期為0001-01-02
ThreadName=Thread-0報錯了 日期字符串:2000-01-01轉換成的日期為0001-01-02
ThreadName=Thread-3報錯了 日期字符串:2000-01-04轉換成的日期為0001-01-02Process finished with exit code 0
- 解決異常辦法一:解決處理錯誤的原理是創建了多個SimpleDateFormat
public class Test {public static void main(String[] args) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String[]dataStringArray = new String[]{"2000-01-01","2000-01-02","2000-01-03","2000-01-04","2000-01-05"};MyThread[] threadArray = new MyThread[5];for (int i=0;i<5;i++) {threadArray[i] = new MyThread(sdf,dataStringArray[i]);}for (int i=0;i<5;i++) {threadArray[i].start();}}}package com.test.formatOk1;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** Created by admin on 2018/12/18.*/
public class DateTools {public static Date parse(String formatPattern, String dataString) throws ParseException{return new SimpleDateFormat(formatPattern).parse(dataString);}public static String format(String formatPattern,Date date){return new SimpleDateFormat(formatPattern).format(date).toString();}}package com.test.formatOk1;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** Created by admin on 2018/12/18.*/
public class MyThread extends Thread {private SimpleDateFormat sdf;private String dateString;public MyThread(SimpleDateFormat sdf,String dateString){super();this.sdf = sdf;this.dateString = dateString;}@Overridepublic void run(){try {Date dateRef = DateTools.parse("yyyy-MM-dd",dateString);String newDateString = DateTools.format("yyyy-MM-dd",dateRef).toString();if (!newDateString.equals(dateString)) {System.out.println("ThreadName="+this.getName()+"報錯了 日期字符串:"+dateString+"轉換成的日期為"+newDateString);}} catch (ParseException e) {e.printStackTrace();}}}
解決異常辦法二:ThreadLocal類能使線程綁定到指定對象,使用ThreadLocal也可以處理這種錯誤
package com.test.formatOk2;import java.text.SimpleDateFormat;/*** SimpleDateFormat非線程安全* Created by admin on 2018/12/18.*/
public class Test {public static void main(String[] args) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String[]dataStringArray = new String[]{"2000-01-01","2000-01-02","2000-01-03","2000-01-04","2000-01-05"};MyThread[] threadArray = new MyThread[5];for (int i=0;i<5;i++) {threadArray[i] = new MyThread(sdf,dataStringArray[i]);}for (int i=0;i<5;i++) {threadArray[i].start();}}}package com.test.formatOk2;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** Created by admin on 2018/12/18.*/
public class DateTools {private static ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<SimpleDateFormat>();public static SimpleDateFormat getSimpleDateFormat(String datePattern){SimpleDateFormat sdf = null;sdf = t1.get();if (sdf == null) {sdf = new SimpleDateFormat(datePattern);t1.set(sdf);}return sdf;}}package com.test.formatOk2;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** Created by admin on 2018/12/18.*/
public class MyThread extends Thread {private SimpleDateFormat sdf;private String dateString;public MyThread(SimpleDateFormat sdf,String dateString){super();this.sdf = sdf;this.dateString = dateString;}@Overridepublic void run(){try {Date dateRef = DateTools.getSimpleDateFormat("yyyy-MM-dd").parse(dateString);String newDateString = DateTools.getSimpleDateFormat("yyyy-MM-dd").format(dateRef).toString();if (!newDateString.equals(dateString)) {System.out.println("ThreadName="+this.getName()+"報錯了 日期字符串:"+dateString+"轉換成的日期為"+newDateString);}} catch (ParseException e) {e.printStackTrace();}}}
轉載于:https://www.cnblogs.com/airycode/p/10135136.html
總結
以上是生活随笔為你收集整理的SimpleDateFormat非线程安全的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。