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

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

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

    ThreadLocal示例

        本文借花獻佛,引用Tim Cull的博文“SimpleDateFormat: Performance Pig”介紹下ThreadLocal的簡單使用,同時也對SimpleDateFormat的使用有個深入的了解。

    Tim Cull 寫道
    Just yesterday I came across this problem “in the wild” for the third time in my career so far: an application with performance problems creating tons of java.text.SimpleDateFormat instances. So, I have to get this out there: creating a new instance of SimpleDateFormat is incredibly expensive and should be minimized. In the case that prompted this post, I was using JProfiler to profile this code that parses a CSV file and discovered that 50% of the time it took to suck in the file and make 55,000 objects out of it was spent solely in the constructor of SimpleDateFormat. It created and then threw away a new one every time it had to parse a date. Whew! 

    “Great,” you think, “I’ll just create one, static instance, slap it in a field in a DateUtils helper class and life will be good.” 

    Well, more precisely, life will be good about 97% of the time. A few days after you roll that code into production you’ll discover the second cool fact that’s good to know: SimpleDateFormat is not thread safe. Your code will work just fine most of the time and all of your regression tests will probably pass, but once your system gets under a production load you’ll see the occasional exception. 

    “Fine,” you think, “I’ll just slap a ’synchronized’ around my use of that one, static instance.” 

    Ok, fine, you could do that and you’d be more or less ok, but the problem is that you’ve now taken a very common operation (date formatting and parsing) and crammed all of your otherwise-lovely, super-parallel application through a single pipe to get it done.


         大致意思:Tim Cull碰到一個SimpleDateFormat帶來的嚴重的性能問題,該問題主要有SimpleDateFormat引發,創建一個SimpleDateFormat實例的開銷比較昂貴,解析字符串時間時頻繁創建生命周期短暫的實例導致性能低下。即使將SimpleDateFormat定義為靜態類變量,貌似能解決這個問題,但是SimpleDateFormat是非線程安全的,同樣存在問題,如果用‘synchronized’線程同步同樣面臨問題,同步導致性能下降(線程之間序列化的獲取SimpleDateFormat實例)。

        Tim Cull使用Threadlocal解決了此問題,對于每個線程SimpleDateFormat不存在影響他們之間協作的狀態,為每個線程創建一個SimpleDateFormat變量的拷貝或者叫做副本,代碼如下:


    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
    * 使用ThreadLocal以空間換時間解決SimpleDateFormat線程安全問題。
    * @author
    *
    */
    public class DateUtil {
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    @SuppressWarnings("rawtypes")
    private static ThreadLocal threadLocal = new ThreadLocal() {
    protected synchronized Object initialValue() {
    return new SimpleDateFormat(DATE_FORMAT);
    }
    };
    public static DateFormat getDateFormat() {
    return (DateFormat) threadLocal.get();
    }
    public static Date parse(String textDate) throws ParseException {
    return getDateFormat().parse(textDate);
    }
    }


       創建一個ThreadLocal類變量,這里創建時用了一個匿名類,覆蓋了initialValue方法,主要作用是創建時初始化實例。也可以采用下面方式創建;


    //第一次調用get將返回null
    private static ThreadLocal threadLocal = new ThreadLocal();
    //獲取線程的變量副本,如果不覆蓋initialValue,第一次get返回null,故需要初始化一個SimpleDateFormat,并set到threadLocal中
    public static DateFormat getDateFormat()
    {
    DateFormat df = (DateFormat) threadLocal.get();
    if(df==null){
    df = new SimpleDateFormat(DATE_FORMAT)
    threadLocal.set(df);
    }
    return df;
    }


       我們看下我們覆蓋的initialValue方法:


    protected T initialValue() {
    return null;//直接返回null
    }




    posted on 2011-05-10 21:01 空白 閱讀(377) 評論(0)  編輯  收藏 所屬分類: Java

    <2011年5月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類(15)

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精品亚洲一区二区麻豆| 亚洲码在线中文在线观看| 亚洲成年网站在线观看| 三年片在线观看免费大全| 亚洲国产成人超福利久久精品| 亚洲黄色免费网址| 亚洲精品免费在线视频| 91黑丝国产线观看免费| 亚洲中文字幕在线无码一区二区| 30岁的女人韩剧免费观看| 亚洲国产精品张柏芝在线观看| AV无码免费永久在线观看| 亚洲1区1区3区4区产品乱码芒果| 岛国av无码免费无禁网站| 亚洲а∨天堂久久精品9966 | 日本特黄a级高清免费大片| 亚洲第一成年免费网站| 国产免费观看a大片的网站| 国产高清对白在线观看免费91| 亚洲男同帅GAY片在线观看| 免费国产黄网站在线观看| 亚洲午夜一区二区电影院| 日本免费观看网站| 中文字幕的电影免费网站| 亚洲精品国产手机| 国产又大又粗又硬又长免费 | 国产成人无码免费看片软件| 亚洲成AV人片一区二区| 久久久www成人免费毛片| 国产精品亚洲精品久久精品| 亚洲午夜未满十八勿入网站2| 青娱乐免费视频在线观看| 久久精品国产亚洲AV未满十八| 亚洲色欲色欲www在线丝| 五月亭亭免费高清在线| 美女裸体无遮挡免费视频网站| 亚洲AV人无码激艳猛片| 午夜dj在线观看免费视频| 免费观看一区二区三区| 国产午夜亚洲精品| 久久夜色精品国产亚洲|