在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  Linux/ 如何理解阿里開發(fā)規(guī)范中這句話?SimpleDateFormat 是線程不安全的類

如何理解阿里開發(fā)規(guī)范中這句話?SimpleDateFormat 是線程不安全的類,一般不要定義為static變量

阿里巴巴JAVA開發(fā)手冊 1.3.1版本中

一編程規(guī)范

(六)并發(fā)處理

5.【強制】SimpleDateFormat 是線程不安全的類,一般不要定義為static變量,如果定義為static,必須加鎖,或者使用DateUtils工具類。

我主要是無法理解后面這句話,“一般不要定義為Static變量”,

為什么?普通的SimpleDateFormat 變量和 Static的SimpleDateFormat 變量在使用上有什么區(qū)別嗎?

各位能理解的大大們能否用代碼舉例說明一下,
將SimpleDateFormat 定義為普通變量和靜態(tài)變量在開發(fā)中會有什么區(qū)別,會遇到什么問題?

回答
編輯回答
祈歡

“一般不要定義為Static變量”,這是為了防止不安全的SimpleDateFormat實例被意外泄漏導致線程安全性問題。但是泄漏實例或者共享實例的方式有很多,不管是否使用static修飾,只要可變實例被多線程共享即不安全。不要定義為static變量只是一種良好的規(guī)范,但并不能阻止實例被多線程共享。

2018年9月22日 09:56
編輯回答
傻丟丟

主要問題在于parse方法,在并發(fā)時,如不同步,會報出以下的異常,導致程序無法正常運行

Exception in thread "Thread-2" Exception in thread "Thread-1" java.lang.NumberFormatException: multiple points
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at java.text.DigitList.getDouble(DigitList.java:169)
    at java.text.DecimalFormat.parse(DecimalFormat.java:2056)

可以用以下代碼片段觸發(fā)異常:

   @Test
    public void testForFail(){
        
        final  SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        
        class MyThread extends Thread{
            int loopCount; String dateString;
            public MyThread(int loops, String dt){
                this.loopCount = loops;
                this.dateString = dt;
            }
            @Override
            public void run() {
                int i = 0;
                while (i++ < loopCount) {        
                    try {
                        Date dt = f.parse(dateString);
                        String s = f.format(dt);    
                        Date res = f.parse(s);
                        assertEquals(res, dt);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        new MyThread(10000,"2018-01-19").start();
        new MyThread(10000,"2017-12-13").start();
        new MyThread(10000,"2019-03-09").start();
        
    }

當只啟動一個線程時,代碼是沒有問題的,但多個線程時很容易出問題。(但也不是每次都出問題;))

另外,即便沒有這個安全問題,供享可被修改內(nèi)部狀態(tài)的實例也會出現(xiàn)意外的結(jié)果。

參考:
https://www.dontpanicblog.co....

2018年8月9日 10:11
編輯回答
舊言
class ThreadDemo extends Thread {
  private static int x=0;
  
  public void run() {
    //...
    test();
  }
  
  public void test () {
    x++;
  }
}

靜態(tài)變量可以被所有ThreadDemo的實例訪問到,因此可能會出現(xiàn)同時訪問的情形。如果沒有做好進程同步的話,可能會出現(xiàn)沖突,因此不是線程安全的。但是非靜態(tài)的變量就是每個實例對應一個了,不會出現(xiàn)多個線程同時訪問的情況。

2017年9月10日 02:28
編輯回答
痞性
SimpleDateFormat 是 Java 中一個非常常用的類,該類用來對日期字符串進行解析和格式化輸出,但如果使用不小心

會導致非常微妙和難以調(diào)試的問題,因為 DateFormat 和 SimpleDateFormat 類不都是線程安全的,在多線程環(huán)境下

調(diào)用 format() 和 parse() 方法應該使用同步代碼來避免問題。通過一個具體的場景來深入理解SimpleDateFormat

類。

一.引子

  在程序中我們應當盡量少的創(chuàng)建SimpleDateFormat 實例,因為創(chuàng)建這么一個實例需要耗費很大的代價。在一個讀取

  

  數(shù)據(jù)庫數(shù)據(jù)導出到excel文件的例子當中,每次處理一個時間信息的時候,就需要創(chuàng)建一個SimpleDateFormat實例對
  
  象,然后再丟棄這個對象。大量的對象就這樣被創(chuàng)建出來,占用大量的內(nèi)存和 jvm空間。
  

代碼如下:

  package com.peidasoft.dateformat;

  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class DateUtil {

      public static  String formatDate(Date date)throws ParseException{
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          return sdf.format(date);
      }

      public static Date parse(String strDate) throws ParseException{
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          return sdf.parse(strDate);
      }
  }

也許會說,OK,那我就創(chuàng)建一個靜態(tài)的simpleDateFormat實例,然后放到一個DateUtil類(如下) 中,在使用時
直接使用這個實例進行操作,這樣問題就解決了。

改進后的代碼如下:

     
  package com.peidasoft.dateformat;

  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class DateUtil {
      private static final  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      public static  String formatDate(Date date)throws ParseException{
          return sdf.format(date);
      }

      public static Date parse(String strDate) throws ParseException{

          return sdf.parse(strDate);
      }
  }

 當然,這個方法的確很不錯,在大部分的時間里面都會工作得很好。但當你在生產(chǎn)環(huán)境中使用一段時間之后,你就會發(fā)
 
現(xiàn)這么一個事實:它不是線程安全的。在正常的測試情況之下,都沒有問題,但一旦在生產(chǎn)環(huán)境中一定負載情況下時,

這個問題就出來了。他會出現(xiàn)各種不同的情況,比如轉(zhuǎn)化的時間不正確,比如報錯,比如線程被掛死等等。我們看下面

的測試用例,那事實說話:

  package com.peidasoft.dateformat;

  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class DateUtil {

      private static final  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      public static  String formatDate(Date date)throws ParseException{
          return sdf.format(date);
      }

      public static Date parse(String strDate) throws ParseException{
  
          return sdf.parse(strDate);
      }
  }


  package com.peidasoft.dateformat;

  import java.text.ParseException;
  import java.util.Date;

  public class DateUtilTest {

      public static class TestSimpleDateFormatThreadSafe extends Thread {
          @Override
    public void run() {
        while(true) {
            try {
                this.join(2000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            try {
                System.out.println(this.getName()+":"+DateUtil.parse("2013-05-2      4 06:02:20"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }    
}


public static void main(String[] args) {
    for(int i = 0; i < 3; i++){
        new TestSimpleDateFormatThreadSafe().start();
    }
        
}

}

執(zhí)行輸出如下:

  Exception in thread "Thread-1" java.lang.NumberFormatException: multiple points
      at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1082)
      at java.lang.Double.parseDouble(Double.java:510)
      at java.text.DigitList.getDouble(DigitList.java:151)
      at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
      at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
      at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
      at java.text.DateFormat.parse(DateFormat.java:335)
      at com.peidasoft.orm.dateformat.DateNoStaticUtil.parse(DateNoStaticUtil.java:17)
      at com.peidasoft.orm. dateformat.DateUtilTest$TestSimpleDateFormatThreadSafe.
      run(DateUtilTest.java:20)
  Exception in thread "Thread-0" java.lang.NumberFormatException: multiple points
      at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1082)
      at java.lang.Double.parseDouble(Double.java:510)
      at java.text.DigitList.getDouble(DigitList.java:151)
      at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
      at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
      at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
      at java.text.DateFormat.parse(DateFormat.java:335)
      at com.peidasoft.orm.dateformat.DateNoStaticUtil.parse(DateNoStaticUtil.java:17)
      at com.peidasoft.orm.dateformat.DateUtilTest$TestSimpleDateFormatThreadSafe.run(DateUtilTest.java:20)
  Thread-2:Mon May:02:20 CST 2021
  Thread-2:Fri May 24 06:02:20 CST 2013
  Thread-2:Fri May 24 06:02:20 CST 2013
  Thread-2:Fri May 24 06:02:20 CST 2013

  說明:Thread-1和Thread-0報java.lang.NumberFormatException: multiple points錯誤,直接掛死,沒起來;Thread-2 雖然沒有掛死,但輸出的時間是有錯誤的,比如我們輸入的時間是:2013-05-24 06:02:20 ,當會輸出:Mon May 24 06:02:20 CST 2021 這樣的靈異事件。

二.原因

  當然都知道,相比于共享一個變量的開銷要比每次創(chuàng)建一個新變量要小很多。上面的優(yōu)化過的靜態(tài)的SimpleDateFormat版,之所在并發(fā)情況下回出現(xiàn)各種靈異錯誤,是因為SimpleDateFormat和DateFormat類不是線程安全的。我們之所以忽視線程安全的問題,是因為從SimpleDateFormat和DateFormat類提供給我們的接口上來看,實在讓人看不出它與線程安全有何相干。只是在JDK文檔的最下面有如下說明:

  SimpleDateFormat中的日期格式不是同步的。推薦(建議)為每個線程創(chuàng)建獨立的格式實例。如果多個線程同時訪問一個格式,則它必須保持外部同步。

JDK原始文檔如下:

   Synchronization:
   Date formats are not synchronized.
   It is recommended to create separate format instances for each thread.
   If multiple threads access a format concurrently, it must be synchronized externally.

下面我們通過看JDK源碼來看看為什么SimpleDateFormat和DateFormat類不是線程安全的真正原因:

  SimpleDateFormat繼承了DateFormat,在DateFormat中定義了一個protected屬性的 Calendar類的對象:calendar。只是因為Calendar累的概念復雜,牽扯到時區(qū)與本地化等等,Jdk的實現(xiàn)中使用了成員變量來傳遞參數(shù),這就造成在多線程的時候會出現(xiàn)錯誤。

在format方法里,有這樣一段代碼:

   private StringBuffer format(Date date, StringBuffer toAppendTo,
                      FieldDelegate delegate) {
    // Convert input date to time field list
          calendar.setTime(date);

boolean useDateFormatSymbols = useDateFormatSymbols();

    for (int i = 0; i < compiledPattern.length; ) {
        int tag = compiledPattern[i] >>> 8;
    int count = compiledPattern[i++] & 0xff;
    if (count == 255) {
    count = compiledPattern[i++] << 16;
    count |= compiledPattern[i++];
    }

    switch (tag) {
    case TAG_QUOTE_ASCII_CHAR:
    toAppendTo.append((char)count);
    break;

    case TAG_QUOTE_CHARS:
    toAppendTo.append(compiledPattern, i, count);
    i += count;
    break;

    default:
            subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
    break;
    }
}
    return toAppendTo;
}

  calendar.setTime(date)這條語句改變了calendar,稍后,calendar還會用到(在subFormat方法里),而這就是引

發(fā)問題的根源。想象一下,在一個多線程環(huán)境下,有兩個線程持有了同一個SimpleDateFormat的實例,分別調(diào)用format方

法:
  線程1調(diào)用format方法,改變了calendar這個字段。

  中斷來了。

  線程2開始執(zhí)行,它也改變了calendar。

  又中斷了。

  線程1回來了,此時,calendar已然不是它所設的值,而是走上了線程2設計的道路。如果多個線程同時爭搶calendar對

象,則會出現(xiàn)各種問題,時間不對,線程掛死等等。

  分析一下format的實現(xiàn),我們不難發(fā)現(xiàn),用到成員變量calendar,唯一的好處,就是在調(diào)用subFormat時,少了一個參

數(shù),卻帶來了這許多的問題。其實,只要在這里用一個局部變量,一路傳遞下去,所有問題都將迎刃而解。

  這個問題背后隱藏著一個更為重要的問題--無狀態(tài):無狀態(tài)方法的好處之一,就是它在各種環(huán)境下,都可以安全的調(diào)用。

衡量一個方法是否是有狀態(tài)的,就看它是否改動了其它的東西,比如全局變量,比如實例的字段。format方法在運行過程中

改動了SimpleDateFormat的calendar字段,所以,它是有狀態(tài)的。

這也同時提醒我們在開發(fā)和設計系統(tǒng)的時候注意下一下三點:

  1.自己寫公用類的時候,要對多線程調(diào)用情況下的后果在注釋里進行明確說明

  2.對線程環(huán)境下,對每一個共享的可變變量都要注意其線程安全性
  
3.我們的類和方法在做設計的時候,要盡量設計成無狀態(tài)的

三.解決辦法

1.需要的時候創(chuàng)建新實例:

  package com.peidasoft.dateformat;

  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class DateUtil {

      public static  String formatDate(Date date)throws ParseException{
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          return sdf.format(date);
      }

      public static Date parse(String strDate) throws ParseException{
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          return sdf.parse(strDate);
      }
  }

  說明:在需要用到SimpleDateFormat 的地方新建一個實例,不管什么時候,將有線程安全問題的對象由共享變?yōu)?/p>

局部私有都能避免多線程問題,不過也加重了創(chuàng)建對象的負擔。在一般情況下,這樣其實對性能影響比不是很明顯的。

2.使用同步:同步SimpleDateFormat對象

  package com.peidasoft.dateformat;

  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class DateSyncUtil {

      private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
     public static String formatDate(Date date)throws ParseException{
          synchronized(sdf){
              return sdf.format(date);
          }  
      }

      public static Date parse(String strDate) throws ParseException{
          synchronized(sdf){
              return sdf.parse(strDate);
          }
      } 
  }

  說明:當線程較多時,當一個線程調(diào)用該方法時,其他想要調(diào)用此方法的線程就要block,多線程并發(fā)量大的時候會對性能有一定的影響。

3.使用ThreadLocal:

  package com.peidasoft.dateformat;

  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class ConcurrentDateUtil {

private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
    @Override
    protected DateFormat initialValue() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
};

public static Date parse(String dateStr) throws ParseException {
    return threadLocal.get().parse(dateStr);
}

public static String format(Date date) {
    return threadLocal.get().format(date);
}

}

另外一種寫法:

  package com.peidasoft.dateformat;

  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;

  public class ThreadLocalDateUtil {
private static final String date_format = "yyyy-MM-dd HH:mm:ss";
private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>(); 
  public static DateFormat getDateFormat()   
{  
    DateFormat df = threadLocal.get();  
    if(df==null){  
        df = new SimpleDateFormat(date_format);  
        threadLocal.set(df);  
    }  
    return df;  
}  

public static String formatDate(Date date) throws ParseException {
    return getDateFormat().format(date);
}

public static Date parse(String strDate) throws ParseException {
    return getDateFormat().parse(strDate);
}   

}

  說明:使用ThreadLocal, 也是將共享變量變?yōu)楠毾?,線程獨享肯定能比方法獨享在并發(fā)環(huán)境中能減少不少創(chuàng)建對象的開銷。如果對性能要求比較高的情況下,一般推薦使用這種方法。

4.拋棄JDK,使用其他類庫中的時間格式化類:

1.使用Apache commons 里的FastDateFormat,宣稱是既快又線程安全的SimpleDateFormat, 可惜它只能對
  日期進行format, 不能對日期串進行解析。

2.使用Joda-Time類庫來處理時間相關問題

  

  做一個簡單的壓力測試,方法一最慢,方法三最快,但是就算是最慢的方法一性能也不差,一般系統(tǒng)方法一和方法二就可
以滿足,所以說在這個點很難成為你系統(tǒng)的瓶頸所在。從簡單的角度來說,建議使用方法一或者方法二,如果在必要的時候,
追求那么一點性能提升的話,可以考慮用方法三,用ThreadLocal做緩存。

  Joda-Time類庫對時間處理方式比較完美,建議使用。
http://blog.csdn.net/zxh87/ar...

2017年12月21日 01:02