<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    DANCE WITH JAVA

    開發出高質量的系統

    常用鏈接

    統計

    積分與排名

    好友之家

    最新評論

    使用SimpleDateFormat必須注意的問題

    在使用SimpleDateFormat的經常會有一些錯誤的用法,例如如下方式:
    public?class?TestDateFormat{
    ?????
    private?SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd");
    ?????
    public?void?method1(){
    ?????????sdf.format(
    new?Date());
    ?????}

    ?????
    public?void?method2(){
    ?????????sdf.format(
    new?Date());
    ?????}

    ?)
    為了漸少new 的次數而把SimpleDateFormat做成成員或者靜態成員,但這樣的做法是隱含著錯誤的,是不
    ?安全的。如下給出證明:
    ?import?java.text.SimpleDateFormat;
    import?java.util.Date;
    import?java.util.HashMap;
    import?java.util.Map;


    public?class?TestDateFormat{
    ????
    private?SimpleDateFormat?sdf?;
    ????
    public?static?void?main(String[]?args)?{
    ????????SimpleDateFormat?sdf
    =?new?SimpleDateFormat("yyyy-MM-dd");
    ????????Date?date1?
    =?new?Date();
    ????????Date?date2?
    =?new?Date(date1.getTime()+1000*60*60*24);
    ????????System.out.println(date1);
    ????????System.out.println(date2);
    ????????Thread?thread1?
    =?new?Thread(new?Thread1(sdf,date1));
    ????????thread1.start();
    ????????Thread?thread2?
    =?new?Thread(new?Thread2(sdf,date2));
    ????????thread2.start();
    ????}

    ????
    }

    class?Thread1?implements?Runnable{
    ????
    private?SimpleDateFormat?sdf;
    ????
    private?Date?date;
    ????
    public?Thread1(SimpleDateFormat?sdf,Date?date){
    ????????
    this.sdf?=?sdf;
    ????????
    this.date?=?date;
    ????}

    ????
    public?void?run()?{
    ????????
    for(;;){
    ????????????String?strDate?
    =?sdf.format(date);
    ????????????
    if("2007-02-27".equals(strDate)){
    ????????????????System.err.println(
    "Error:?date1="+strDate);
    ????????????????System.exit(
    0);
    ????????????}

    ????????}

    ????}

    }

    class?Thread2?implements?Runnable{
    ????
    private?SimpleDateFormat?sdf;
    ????
    private?Date?date;
    ????
    public?Thread2(SimpleDateFormat?sdf,Date?date){
    ????????
    this.sdf?=?sdf;
    ????????
    this.date?=?date;
    ????}

    ????
    public?void?run()?{
    ????????
    for(;;){
    ????????????String?strDate?
    =?sdf.format(date);
    ????????????
    if("2007-02-26".equals(strDate)){
    ????????????????System.err.println(
    "Error:?date1="+strDate);
    ????????????????System.exit(
    0);
    ????????????}

    ????????}

    ????}

    }

    很快,基本幾十次就會出現錯誤。錯誤原因:
    ?因為SimpleDateFormat處理復雜,Jdk的實現中使用了成員變量來傳遞參數,這個就造成在多線程的時候
    ?會出現錯誤。上邊的程序證明了這個問題。
    ?
    ?再看看SimpleDateFormat的Jdk 的Source,有這么一段注釋,說明它不是線程安全的。
    ?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
    ?
    ?繼續看看Sun自己的網站上。在sun的bug database中
    ?Bug ID:? 4228335? 講的就是這個問題。
    ?SimpleDateFormat is not threadsafe (one more try)
    ?其中有這么幾段話值得關注:
    ?1,
    ?Aside from the obvious, there are two reasons why this fix should be made:
    - The documentation for DateFormat states that a DateFormat object should be used
    multiple times, implying that construction is expensive.? Furthermore,
    ?no mention is made of SimpleDateFormat's lack of thread-safety.?
    ?Since for most applications the date formats remain constant,
    ?it is very easy to conclude that DateFormats should be application globals.
    ?But SimpleDateFormat produces incorrect results when used in this way.
    - Bug 4101500, a similar problem with NumberFormat, was fixed.

    建議修改這個問題,而且NumberFormat已經修改,解決了這個問題。簡單測試了一下NumberFormat確是不出錯

    2,
    Use of a cloned Calendar object in format(), as suggested in JDC comments,
    slows down the execution 30-50%. And it potentially affects footprint because
    of cloned objects. Another approach would be to have a Calendar instance per
    thread. However, this approach will cause problems at the API semantic level due
    to the get/setCalendar methods.
    這一段解釋了為什么沒修改這個問題,一個是保持API不變,另一個是因為Clone比new慢,會損失效率
    關于clone與new的快慢,看這里http://www.tkk7.com/dreamstone/archive/2007/02/26/100761.html

    其它的還有好多,感興趣的自己看看吧。

    結論:每次使用它的時候現new,或者用一個同步函數把new好的包裝起來使用吧。

    posted on 2007-02-26 16:03 dreamstone 閱讀(2876) 評論(0)  編輯  收藏 所屬分類: jdk相關

    主站蜘蛛池模板: 亚洲第一福利网站| 97无码免费人妻超级碰碰碰碰| 四虎亚洲精品高清在线观看| 亚洲欧洲国产精品香蕉网| 国产精品免费播放| 国产免费av片在线看| 6080午夜一级毛片免费看 | 中文在线观看国语高清免费| 亚洲欧洲免费无码| 亚洲国产视频网站| 78成人精品电影在线播放日韩精品电影一区亚洲 | 最近2018中文字幕免费视频| 最近中文字幕免费大全| 免费国产高清毛不卡片基地| 亚洲熟妇丰满xxxxx| 亚洲1区1区3区4区产品乱码芒果 | 日韩在线视频免费| 亚洲av无码成人精品国产| 亚洲熟女乱色一区二区三区| 国产精品高清视亚洲精品| 亚洲婷婷天堂在线综合| 亚洲精品视频免费看| 亚洲视频一区网站| 亚洲视频免费在线播放| 亚洲最大视频网站| 久久亚洲国产精品成人AV秋霞| 图图资源网亚洲综合网站| 久久精品国产亚洲AV麻豆不卡| 久久国产亚洲精品麻豆| 亚洲av永久无码精品网站| 国产亚洲婷婷香蕉久久精品 | 国产区在线免费观看| 国产免费一区二区三区免费视频 | 亚洲人成网77777色在线播放| 亚洲男女内射在线播放| 亚洲一区二区三区在线播放| 中文字幕亚洲电影| 亚洲成av人影院| 久久亚洲sm情趣捆绑调教| 亚洲婷婷在线视频| 亚洲欧美成人av在线观看|