SimpleDateFormat-多線程問題


SimpleDateFormat-多線程問題:

  SimpleDateFormat類在多線程環境下中處理日期,極易出現日期轉換錯誤的情況

 1 import java.text.ParseException;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 
 5 /**
 6  *    線程類
 7  */
 8 public class MyThread extends Thread {
 9     
10     private SimpleDateFormat sdf;
11     private String dateString;
12     
13     public MyThread(SimpleDateFormat sdf,String dateString) {
14         this.sdf = sdf;
15         this.dateString = dateString;
16     }
17     
18     @Override
19     public void run() {
20         try {
21             Date dateRef = sdf.parse(dateString);
22             String newDateString = sdf.format(dateRef).toString();
23             if(!newDateString.equals(dateString)) {
24                 System.out.println("ThreadName = " + Thread.currentThread().getName() 
25                         + "報錯了  日期字符串:" + dateString + "轉換成日期為:" + newDateString);
26             }
27         } catch (ParseException e) {
28             e.printStackTrace();
29         }
30     }
31 }
 1 import java.text.SimpleDateFormat;
 2 
 3 public class Test {
 4 
 5     /**
 6      *    測試單例的SimpleDateFormat類在多線程環境下中處理日期,極易出現日期轉換錯誤的情況
 7      */
 8     public static void main(String[] args) {
 9         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
10         String[] dateStringArray = new String[] {
11                 "2000-01-01","2000-01-02","2000-01-03",
12                 "2000-01-04","2000-01-05","2000-01-06",
13                 "2000-01-07","2000-01-08","2000-01-09",
14                 "2000-01-10"
15         };
16         
17         MyThread[] threadArray = new MyThread[10];
18         for (int i = 0; i < 10; i++) {
19             threadArray[i] = new MyThread(sdf, dateStringArray[i]);
20         }
21         for (int i = 0; i < 10; i++) {
22             threadArray[i].start();
23         }
24     }
25 }

運行之后會輸出很多的錯誤信息!

 

  解決多線程出現的問題-為每個線程實例一個SimpleDateFormat:

 1 import java.text.ParseException;
 2 import java.util.Date;
 3 
 4 /**
 5  *    線程類
 6  */
 7 public class MyThread extends Thread {
 8     
 9     private String dateString;
10     public MyThread(String dateString) {
11         this.dateString = dateString;
12     }
13     
14     @Override
15     public void run() {
16         try {
17             Date dateRef = DateTools.parse("yyyy-MM-dd", dateString);
18             String newDateString = DateTools.format("yyyy-MM-dd",dateRef).toString();
19             if(!newDateString.equals(dateString)) {
20                 System.out.println("ThreadName = " + Thread.currentThread().getName() 
21                         + "報錯了  日期字符串:" + dateString + "轉換成日期為:" + newDateString);
22             }
23         } catch (ParseException e) {
24             e.printStackTrace();
25         }
26     }
27 }
 1 import java.text.ParseException;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 
 5 /**
 6  *    日期格式化工具類
 7  */
 8 public class DateTools {
 9     
10     public static Date parse(String formatPattern,String dateString) throws ParseException {
11         return new SimpleDateFormat(formatPattern).parse(dateString);
12     }
13 
14     public static String format(String formatPattern,Date date) throws ParseException {
15         return new SimpleDateFormat(formatPattern).format(date).toString();
16     }
17 }
 1 public class Test {
 2 
 3     /**
 4      *    測試,運行程序后,控制台沒有任何輸出,也就是轉換沒有任何異常,
 5      *    原理:創建了多個SimpleDateFormat實例
 6      */
 7     public static void main(String[] args) {
 8         String[] dateStringArray = new String[] {
 9                 "2000-01-01","2000-01-02","2000-01-03",
10                 "2000-01-04","2000-01-05","2000-01-06",
11                 "2000-01-07","2000-01-08","2000-01-09",
12                 "2000-01-10"
13         };
14         
15         MyThread[] threadArray = new MyThread[10];
16         for (int i = 0; i < 10; i++) {
17             threadArray[i] = new MyThread(dateStringArray[i]);
18         }
19         for (int i = 0; i < 10; i++) {
20             threadArray[i].start();
21         }
22     }
23 }

  解決多線程出現的問題-使用ThreadLocal:

 1 import java.text.ParseException;
 2 import java.util.Date;
 3 
 4 /**
 5  *    線程類
 6  */
 7 public class MyThread extends Thread {
 8     
 9     private String dateString;
10     public MyThread(String dateString) {
11         this.dateString = dateString;
12     }
13     
14     @Override
15     public void run() {
16         try {
17             Date dateRef = DateTools.getSimpleDateFormat("yyyy-MM-dd").parse(dateString);
18             String newDateString = DateTools.getSimpleDateFormat("yyyy-MM-dd").format(dateRef).toString();
19             if(!newDateString.equals(dateString)) {
20                 System.out.println("ThreadName = " + Thread.currentThread().getName() 
21                         + "報錯了  日期字符串:" + dateString + "轉換成日期為:" + newDateString);
22             }
23         } catch (ParseException e) {
24             e.printStackTrace();
25         }
26     }
27 }
 1 import java.text.SimpleDateFormat;
 2 
 3 /**
 4  *    日期格式化工具類,使用ThreadLocal解決SimpleDateFormat非線程安全問題
 5  */
 6 public class DateTools {
 7     
 8     private static ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<>();
 9     
10     public static SimpleDateFormat getSimpleDateFormat(String datePattern) {
11         SimpleDateFormat sdf = null;
12         sdf = t1.get();
13         if(sdf == null) {
14             sdf = new SimpleDateFormat(datePattern);
15             t1.set(sdf);
16         }
17         return sdf;
18     }
19 }
 1 public class Test {
 2 
 3     /**
 4      *    測試,運行程序后,控制台沒有任何輸出,也就是轉換沒有任何異常
 5      *    原理:每個線程都會有自己的ThreadLocal存儲全局變量,也就是每個線程都有自己的SimpleDateFormat實例
 6      */
 7     public static void main(String[] args) {
 8         String[] dateStringArray = new String[] {
 9                 "2000-01-01","2000-01-02","2000-01-03",
10                 "2000-01-04","2000-01-05","2000-01-06",
11                 "2000-01-07","2000-01-08","2000-01-09",
12                 "2000-01-10"
13         };
14         
15         MyThread[] threadArray = new MyThread[10];
16         for (int i = 0; i < 10; i++) {
17             threadArray[i] = new MyThread(dateStringArray[i]);
18         }
19         for (int i = 0; i < 10; i++) {
20             threadArray[i].start();
21         }
22     }
23 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM