1. 程式人生 > >小編帶你了解SimpleDateFormat-多線程問題

小編帶你了解SimpleDateFormat-多線程問題

oca tostring == tool 工具類 getname ref @override ret

SimpleDateFormat-多線程問題:
  SimpleDateFormat類在多線程環境下中處理日期,極易出現日期轉換錯誤的情況
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**

  • 線程類
    */
    public class MyThread extends Thread {

    private SimpleDateFormat sdf;
    private String dateString;

    public MyThread(SimpleDateFormat sdf,String dateString) {

    this.sdf = sdf;
    this.dateString = dateString;
    }

    @Override
    public void run() {
    try {
    Date dateRef = sdf.parse(dateString);
    String newDateString = sdf.format(dateRef).toString();
    if(!newDateString.equals(dateString)) {
    System.out.println("ThreadName = " + Thread.currentThread().getName()

    • "報錯了 日期字符串:" + dateString + "轉換成日期為:" + newDateString);
      }
      } catch (ParseException e) {
      e.printStackTrace();
      }
      }
      }
      import java.text.SimpleDateFormat;

public class Test {

/**
 *    測試單例的SimpleDateFormat類在多線程環境下中處理日期,極易出現日期轉換錯誤的情況
 */
public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String[] dateStringArray = new String[] {
            "2000-01-01","2000-01-02","2000-01-03",
            "2000-01-04","2000-01-05","2000-01-06",
            "2000-01-07","2000-01-08","2000-01-09",
            "2000-01-10"
    };

    MyThread[] threadArray = new MyThread[10];
    for (int i = 0; i < 10; i++) {
        threadArray[i] = new MyThread(sdf, dateStringArray[i]);
    }
    for (int i = 0; i < 10; i++) {
        threadArray[i].start();
    }
}

}
運行之後會輸出很多的錯誤信息!
解決多線程出現的問題-為每個線程實例一個SimpleDateFormat:
import java.text.ParseException;
import java.util.Date;

/**

  • 線程類
    */
    public class MyThread extends Thread {

    private String dateString;
    public MyThread(String dateString) {
    this.dateString = dateString;
    }

    @Override
    public 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 = " + Thread.currentThread().getName()

    • "報錯了 日期字符串:" + dateString + "轉換成日期為:" + newDateString);
      }
      } catch (ParseException e) {
      e.printStackTrace();
      }
      }
      }
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;

/**

  • 日期格式化工具類
    */
    public class DateTools {

    public static Date parse(String formatPattern,String dateString) throws ParseException {
    return new SimpleDateFormat(formatPattern).parse(dateString);
    }

    public static String format(String formatPattern,Date date) throws ParseException {
    return new SimpleDateFormat(formatPattern).format(date).toString();
    }
    }
    public class Test {

    /**

    • 測試,運行程序後,控制臺沒有任何輸出,也就是轉換沒有任何異常,
    • 原理:創建了多個SimpleDateFormat實例
      */
      public static void main(String[] args) {
      String[] dateStringArray = new String[] {
      "2000-01-01","2000-01-02","2000-01-03",
      "2000-01-04","2000-01-05","2000-01-06",
      "2000-01-07","2000-01-08","2000-01-09",
      "2000-01-10"
      };

      MyThread[] threadArray = new MyThread[10];
      for (int i = 0; i < 10; i++) {
      threadArray[i] = new MyThread(dateStringArray[i]);
      }
      for (int i = 0; i < 10; i++) {
      threadArray[i].start();
      }
      }
      }
      解決多線程出現的問題-使用ThreadLocal:
      import java.text.ParseException;
      import java.util.Date;

/**

  • 線程類
    */
    public class MyThread extends Thread {

    private String dateString;
    public MyThread(String dateString) {
    this.dateString = dateString;
    }

    @Override
    public 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 = " + Thread.currentThread().getName()

    • "報錯了 日期字符串:" + dateString + "轉換成日期為:" + newDateString);
      }
      } catch (ParseException e) {
      e.printStackTrace();
      }
      }
      }
      import java.text.SimpleDateFormat;

/**

  • 日期格式化工具類,使用ThreadLocal解決SimpleDateFormat非線程安全問題
    */
    public class DateTools {

    private static ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<>();

    public static SimpleDateFormat getSimpleDateFormat(String datePattern) {
    SimpleDateFormat sdf = null;
    sdf = t1.get();
    if(sdf == null) {
    sdf = new SimpleDateFormat(datePattern);
    t1.set(sdf);
    }
    return sdf;
    }
    }
    public class Test {

    /**

    • 測試,運行程序後,控制臺沒有任何輸出,也就是轉換沒有任何異常
    • 原理:每個線程都會有自己的ThreadLocal存儲全局變量,也就是每個線程都有自己的SimpleDateFormat實例
      */
      public static void main(String[] args) {
      String[] dateStringArray = new String[] {
      "2000-01-01","2000-01-02","2000-01-03",
      "2000-01-04","2000-01-05","2000-01-06",
      "2000-01-07","2000-01-08","2000-01-09",
      "2000-01-10"
      };

      MyThread[] threadArray = new MyThread[10];
      for (int i = 0; i < 10; i++) {
      threadArray[i] = new MyThread(dateStringArray[i]);
      }
      for (int i = 0; i < 10; i++) {
      threadArray[i].start();
      }
      }
      }

小編帶你了解SimpleDateFormat-多線程問題