亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

正確理解ThreadLocal

系統(tǒng) 2028 0

首先,ThreadLocal 不是用來解決共享對象的多線程訪問問題的,一般情況下,通過ThreadLocal.set() 到線程中的對象是該線程自己使用的對象,其他線程是不需要訪問的,也訪問不到的。 各個線程中訪問的是不同的對象。

另外,說ThreadLocal使得各線程能夠保持各自獨立的一個對象,并不是通過ThreadLocal.set()來實現的,而是通過每個線程中的new 對象 的操作來創(chuàng)建的對象,每個線程創(chuàng)建一個,不是什么對象的拷貝或副本。 通過ThreadLocal.set()將這個新創(chuàng)建的對象的引用保存到各線程的自己的一個map中,每個線程都有這樣一個map,執(zhí)行ThreadLocal.get()時,各線程從自己的map中取出放進去的對象,因此取出來的是各自自己線程中的對象,ThreadLocal實例是作為map的key來使用的。

如果ThreadLocal.set()進去的東西本來就是多個線程共享的同一個對象,那么多個線程的ThreadLocal.get()取得的還是這個共享對象本身,還是有并發(fā)訪問問題。

下面來看一個hibernate中典型的ThreadLocal的應用:

Java代碼 復制代碼
  1. private ? static ? final ?ThreadLocal?threadSession?=? new ?ThreadLocal(); ??
  2. ??
  3. public ? static ?Session?getSession()? throws ?InfrastructureException?{ ??
  4. ????Session?s?=?(Session)?threadSession.get(); ??
  5. ???? try ?{ ??
  6. ???????? if ?(s?==? null )?{ ??
  7. ????????????s?=?getSessionFactory().openSession(); ??
  8. ????????????threadSession.set(s); ??
  9. ????????} ??
  10. ????}? catch ?(HibernateException?ex)?{ ??
  11. ???????? throw ? new ?InfrastructureException(ex); ??
  12. ????} ??
  13. ???? return ?s; ??
  14. }??
          private static final ThreadLocal threadSession = new ThreadLocal();

    public static Session getSession() throws InfrastructureException {
        Session s = (Session) threadSession.get();
        try {
            if (s == null) {
                s = getSessionFactory().openSession();
                threadSession.set(s);
            }
        } catch (HibernateException ex) {
            throw new InfrastructureException(ex);
        }
        return s;
    }

    


可以看到在getSession()方法中,首先判斷當前線程中有沒有放進去session,如果還沒有,那么通過sessionFactory().openSession()來創(chuàng)建一個session,再將session set到線程中,實際是放到當前線程的ThreadLocalMap這個map中,這時,對于這個session的唯一引用就是當前線程中的那個ThreadLocalMap(下面會講到),而threadSession作為這個值的key,要取得這個session可以通過threadSession.get()來得到,里面執(zhí)行的操作實際是先取得當前線程中的ThreadLocalMap,然后將threadSession作為key將對應的值取出。這個session相當于線程的私有變量,而不是public的。
顯然,其他線程中是取不到這個session的,他們也只能取到自己的ThreadLocalMap中的東西。要是session是多個線程共享使用的,那還不亂套了。
試想如果不用ThreadLocal怎么來實現呢?可能就要在action中創(chuàng)建session,然后把session一個個傳到service和dao中,這可夠麻煩的?;蛘呖梢宰约憾x一個靜態(tài)的map,將當前thread作為key,創(chuàng)建的session作為值,put到map中,應該也行,這也是一般人的想法,但事實上,ThreadLocal的實現剛好相反,它是在每個線程中有一個map,而將ThreadLocal實例作為key,這樣每個map中的項數很少,而且當線程銷毀時相應的東西也一起銷毀了,不知道除了這些還有什么其他的好處。

總之,ThreadLocal不是用來解決對象共享訪問問題的,而主要是提供了保持對象的方法和避免參數傳遞的方便的對象訪問方式。歸納了兩點:
1。每個線程中都有一個自己的ThreadLocalMap類對象,可以將線程自己的對象保持到其中,各管各的,線程可以正確的訪問到自己的對象。
2。將一個共用的ThreadLocal靜態(tài)實例作為key,將不同對象的引用保存到不同線程的ThreadLocalMap中,然后在線程執(zhí)行的各處通過這個靜態(tài)ThreadLocal實例的get()方法取得自己線程保存的那個對象,避免了將這個對象作為參數傳遞的麻煩。


當然如果要把本來線程共享的對象通過ThreadLocal.set()放到線程中也可以,可以實現避免參數傳遞的訪問方式,但是要注意get()到的是那同一個共享對象,并發(fā)訪問問題要靠其他手段來解決。但一般來說線程共享的對象通過設置為某類的靜態(tài)變量就可以實現方便的訪問了,似乎沒必要放到線程中。

ThreadLocal的應用場合,我覺得最適合的是按線程多實例(每個線程對應一個實例)的對象的訪問,并且這個對象很多地方都要用到。

下面來看看ThreadLocal的實現原理(jdk1.5源碼)

Java代碼 復制代碼
  1. public ? class ?ThreadLocal<T>?{ ??
  2. ???? /** ?
  3. ?????*?ThreadLocals?rely?on?per-thread?hash?maps?attached?to?each?thread ?
  4. ?????*?(Thread.threadLocals?and?inheritableThreadLocals).??The?ThreadLocal ?
  5. ?????*?objects?act?as?keys,?searched?via?threadLocalHashCode.??This?is?a ?
  6. ?????*?custom?hash?code?(useful?only?within?ThreadLocalMaps)?that?eliminates ?
  7. ?????*?collisions?in?the?common?case?where?consecutively?constructed ?
  8. ?????*?ThreadLocals?are?used?by?the?same?threads,?while?remaining?well-behaved ?
  9. ?????*?in?less?common?cases. ?
  10. ?????*/ ??
  11. ???? private ? final ? int ?threadLocalHashCode?=?nextHashCode(); ??
  12. ??
  13. ???? /** ?
  14. ?????*?The?next?hash?code?to?be?given?out.?Accessed?only?by?like-named?method. ?
  15. ?????*/ ??
  16. ???? private ? static ? int ?nextHashCode?=? 0 ; ??
  17. ??
  18. ???? /** ?
  19. ?????*?The?difference?between?successively?generated?hash?codes?-?turns ?
  20. ?????*?implicit?sequential?thread-local?IDs?into?near-optimally?spread ?
  21. ?????*?multiplicative?hash?values?for?power-of-two-sized?tables. ?
  22. ?????*/ ??
  23. ???? private ? static ? final ? int ?HASH_INCREMENT?=? 0x61c88647 ; ??
  24. ??
  25. ???? /** ?
  26. ?????*?Compute?the?next?hash?code.?The?static?synchronization?used?here ?
  27. ?????*?should?not?be?a?performance?bottleneck.?When?ThreadLocals?are ?
  28. ?????*?generated?in?different?threads?at?a?fast?enough?rate?to?regularly ?
  29. ?????*?contend?on?this?lock,?memory?contention?is?by?far?a?more?serious ?
  30. ?????*?problem?than?lock?contention. ?
  31. ?????*/ ??
  32. ???? private ? static ? synchronized ? int ?nextHashCode()?{ ??
  33. ???????? int ?h?=?nextHashCode; ??
  34. ????????nextHashCode?=?h?+?HASH_INCREMENT; ??
  35. ???????? return ?h; ??
  36. ????} ??
  37. ??
  38. ???? /** ?
  39. ?????*?Creates?a?thread?local?variable. ?
  40. ?????*/ ??
  41. ???? public ?ThreadLocal()?{ ??
  42. ????} ??
  43. ??
  44. ???? /** ?
  45. ?????*?Returns?the?value?in?the?current?thread's?copy?of?this?thread-local ?
  46. ?????*?variable.??Creates?and?initializes?the?copy?if?this?is?the?first?time ?
  47. ?????*?the?thread?has?called?this?method. ?
  48. ?????* ?
  49. ?????*?@return?the?current?thread's?value?of?this?thread-local ?
  50. ?????*/ ??
  51. ???? public ?T?get()?{ ??
  52. ????????Thread?t?=?Thread.currentThread(); ??
  53. ????????ThreadLocalMap?map?=?getMap(t); ??
  54. ???????? if ?(map?!=? null ) ??
  55. ???????????? return ?(T)map.get( this ); ??
  56. ??
  57. ???????? //?Maps?are?constructed?lazily.??if?the?map?for?this?thread ??
  58. ???????? //?doesn't?exist,?create?it,?with?this?ThreadLocal?and?its ??
  59. ???????? //?initial?value?as?its?only?entry. ??
  60. ????????T?value?=?initialValue(); ??
  61. ????????createMap(t,?value); ??
  62. ???????? return ?value; ??
  63. ????} ??
  64. ??
  65. ???? /** ?
  66. ?????*?Sets?the?current?thread's?copy?of?this?thread-local?variable ?
  67. ?????*?to?the?specified?value.??Many?applications?will?have?no?need?for ?
  68. ?????*?this?functionality,?relying?solely?on?the?{@link?#initialValue} ?
  69. ?????*?method?to?set?the?values?of?thread-locals. ?
  70. ?????* ?
  71. ?????*?@param?value?the?value?to?be?stored?in?the?current?threads'?copy?of ?
  72. ?????*????????this?thread-local. ?
  73. ?????*/ ??
  74. ???? public ? void ?set(T?value)?{ ??
  75. ????????Thread?t?=?Thread.currentThread(); ??
  76. ????????ThreadLocalMap?map?=?getMap(t); ??
  77. ???????? if ?(map?!=? null ) ??
  78. ????????????map.set( this ,?value); ??
  79. ???????? else ??
  80. ????????????createMap(t,?value); ??
  81. ????} ??
  82. ??
  83. ???? /** ?
  84. ?????*?Get?the?map?associated?with?a?ThreadLocal.?Overridden?in ?
  85. ?????*?InheritableThreadLocal. ?
  86. ?????* ?
  87. ?????*?@param??t?the?current?thread ?
  88. ?????*?@return?the?map ?
  89. ?????*/ ??
  90. ????ThreadLocalMap?getMap(Thread?t)?{ ??
  91. ???????? return ?t.threadLocals; ??
  92. ????} ??
  93. ??
  94. ???? /** ?
  95. ?????*?Create?the?map?associated?with?a?ThreadLocal.?Overridden?in ?
  96. ?????*?InheritableThreadLocal. ?
  97. ?????* ?
  98. ?????*?@param?t?the?current?thread ?
  99. ?????*?@param?firstValue?value?for?the?initial?entry?of?the?map ?
  100. ?????*?@param?map?the?map?to?store. ?
  101. ?????*/ ??
  102. ???? void ?createMap(Thread?t,?T?firstValue)?{ ??
  103. ????????t.threadLocals?=? new ?ThreadLocalMap( this ,?firstValue); ??
  104. ????} ??
  105. ??
  106. ????....... ??
  107. ??
  108. ???? /** ?
  109. ?????*?ThreadLocalMap?is?a?customized?hash?map?suitable?only?for ?
  110. ?????*?maintaining?thread?local?values.?No?operations?are?exported ?
  111. ?????*?outside?of?the?ThreadLocal?class.?The?class?is?package?private?to ?
  112. ?????*?allow?declaration?of?fields?in?class?Thread.??To?help?deal?with ?
  113. ?????*?very?large?and?long-lived?usages,?the?hash?table?entries?use ?
  114. ?????*?WeakReferences?for?keys.?However,?since?reference?queues?are?not ?
  115. ?????*?used,?stale?entries?are?guaranteed?to?be?removed?only?when ?
  116. ?????*?the?table?starts?running?out?of?space. ?
  117. ?????*/ ??
  118. ???? static ? class ?ThreadLocalMap?{ ??
  119. ??
  120. ????........ ??
  121. ??
  122. ????} ??
  123. ??
  124. }??
      public class ThreadLocal<T> {
    /**
     * ThreadLocals rely on per-thread hash maps attached to each thread
     * (Thread.threadLocals and inheritableThreadLocals).  The ThreadLocal
     * objects act as keys, searched via threadLocalHashCode.  This is a
     * custom hash code (useful only within ThreadLocalMaps) that eliminates
     * collisions in the common case where consecutively constructed
     * ThreadLocals are used by the same threads, while remaining well-behaved
     * in less common cases.
     */
    private final int threadLocalHashCode = nextHashCode();

    /**
     * The next hash code to be given out. Accessed only by like-named method.
     */
    private static int nextHashCode = 0;

    /**
     * The difference between successively generated hash codes - turns
     * implicit sequential thread-local IDs into near-optimally spread
     * multiplicative hash values for power-of-two-sized tables.
     */
    private static final int HASH_INCREMENT = 0x61c88647;

    /**
     * Compute the next hash code. The static synchronization used here
     * should not be a performance bottleneck. When ThreadLocals are
     * generated in different threads at a fast enough rate to regularly
     * contend on this lock, memory contention is by far a more serious
     * problem than lock contention.
     */
    private static synchronized int nextHashCode() {
        int h = nextHashCode;
        nextHashCode = h + HASH_INCREMENT;
        return h;
    }

    /**
     * Creates a thread local variable.
     */
    public ThreadLocal() {
    }

    /**
     * Returns the value in the current thread's copy of this thread-local
     * variable.  Creates and initializes the copy if this is the first time
     * the thread has called this method.
     *
     * @return the current thread's value of this thread-local
     */
    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            return (T)map.get(this);

        // Maps are constructed lazily.  if the map for this thread
        // doesn't exist, create it, with this ThreadLocal and its
        // initial value as its only entry.
        T value = initialValue();
        createMap(t, value);
        return value;
    }

    /**
     * Sets the current thread's copy of this thread-local variable
     * to the specified value.  Many applications will have no need for
     * this functionality, relying solely on the {@link #initialValue}
     * method to set the values of thread-locals.
     *
     * @param value the value to be stored in the current threads' copy of
     *        this thread-local.
     */
    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

    /**
     * Get the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     *
     * @param  t the current thread
     * @return the map
     */
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

    /**
     * Create the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     *
     * @param t the current thread
     * @param firstValue value for the initial entry of the map
     * @param map the map to store.
     */
    void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

    .......

    /**
     * ThreadLocalMap is a customized hash map suitable only for
     * maintaining thread local values. No operations are exported
     * outside of the ThreadLocal class. The class is package private to
     * allow declaration of fields in class Thread.  To help deal with
     * very large and long-lived usages, the hash table entries use
     * WeakReferences for keys. However, since reference queues are not
     * used, stale entries are guaranteed to be removed only when
     * the table starts running out of space.
     */
    static class ThreadLocalMap {

    ........

    }

}
    



可以看到ThreadLocal類中的變量只有這3個int型:

Java代碼 復制代碼
  1. private ? final ? int ?threadLocalHashCode?=?nextHashCode(); ??
  2. private ? static ? int ?nextHashCode?=? 0 ; ??
  3. private ? static ? final ? int ?HASH_INCREMENT?=? 0x61c88647 ;??
          private final int threadLocalHashCode = nextHashCode();
    private static int nextHashCode = 0;
    private static final int HASH_INCREMENT = 0x61c88647;
    


而作為ThreadLocal實例的變量只有 threadLocalHashCode 這一個,nextHashCode 和HASH_INCREMENT 是ThreadLocal類的靜態(tài)變量,實際上HASH_INCREMENT是一個常量,表示了連續(xù)分配的兩個ThreadLocal實例的threadLocalHashCode值的增量,而nextHashCode 的表示了即將分配的下一個ThreadLocal實例的threadLocalHashCode 的值。

可以來看一下創(chuàng)建一個ThreadLocal實例即new ThreadLocal()時做了哪些操作,從上面看到構造函數ThreadLocal()里什么操作都沒有,唯一的操作是這句:

Java代碼 復制代碼
  1. private ? final ? int ?threadLocalHashCode?=?nextHashCode();??
          private final int threadLocalHashCode = nextHashCode();
    


那么nextHashCode()做了什么呢:

Java代碼 復制代碼
  1. private ? static ? synchronized ? int ?nextHashCode()?{ ??
  2. ???? int ?h?=?nextHashCode; ??
  3. ????nextHashCode?=?h?+?HASH_INCREMENT; ??
  4. ???? return ?h; ??
  5. }??
          private static synchronized int nextHashCode() {
        int h = nextHashCode;
        nextHashCode = h + HASH_INCREMENT;
        return h;
    }
    

就是將ThreadLocal類的下一個hashCode值即nextHashCode的值賦給實例的threadLocalHashCode,然后nextHashCode的值增加HASH_INCREMENT這個值。

因此ThreadLocal實例的變量只有這個threadLocalHashCode,而且是final的,用來區(qū)分不同的ThreadLocal實例,ThreadLocal類主要是作為工具類來使用,那么ThreadLocal.set()進去的對象是放在哪兒的呢?

看一下上面的set()方法,兩句合并一下成為

Java代碼 復制代碼
  1. ThreadLocalMap?map?=?Thread.currentThread().threadLocals;??
              ThreadLocalMap map = Thread.currentThread().threadLocals;
    


這個ThreadLocalMap 類是ThreadLocal中定義的內部類,但是它的實例卻用在Thread類中:

Java代碼 復制代碼
  1. public ? class ?Thread? implements ?Runnable?{ ??
  2. ????...... ??
  3. ??
  4. ???? /*?ThreadLocal?values?pertaining?to?this?thread.?This?map?is?maintained ?
  5. ?????*?by?the?ThreadLocal?class.?*/ ??
  6. ????ThreadLocal.ThreadLocalMap?threadLocals?=? null ;?? ??
  7. ????...... ??
  8. }??
      public class Thread implements Runnable {
    ......

    /* ThreadLocal values pertaining to this thread. This map is maintained
     * by the ThreadLocal class. */
    ThreadLocal.ThreadLocalMap threadLocals = null;  
    ......
}
    



再看這句:

Java代碼 復制代碼
  1. if ?(map?!=? null ) ??
  2. ????map.set( this ,?value);??
              if (map != null)
            map.set(this, value);
    


也就是將該ThreadLocal實例作為key,要保持的對象作為值,設置到當前線程的ThreadLocalMap 中,get()方法同樣大家看了代碼也就明白了,ThreadLocalMap 類的代碼太多了,我就不帖了,自己去看源碼吧。

正確理解ThreadLocal


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品亚洲一区二区三区在线播放 | 日韩中文字幕免费观看 | 爽爽影院免费观看视频 | 久久综合视频网站 | 四虎国产精品永久地址99 | 国产精品久久久久久五月尺 | 二级毛片在线观看 | 亚洲va欧美va国产va天堂影 | 狠狠色噜噜狠狠狠狠91 | 宅男在线看片 | 国产成人一级 | 欧美人与动人物a级网站 | 欧美日韩高清 | 99热成人精品热久久669 | 国内免费一区二区三区视频 | 亚洲国产日韩欧美mv | 婷婷色中文 | a网站免费 | 麻豆成人精品国产免费 | 俄罗斯色视频 | 五月国产综合视频在线观看 | 精品国产精品国产 | 国内精品视频 | 久久久午夜影院 | 国产精品日韩欧美一区二区三区 | 日日搞夜夜操 | 久久性生大片免费观看性 | 天天爽夜夜爽精品视频一 | 亚洲欧美日韩国产一区图片 | 视频国产在线 | 综合伊人久久 | 天天夜天天干 | www久久精品 | 91一区二区三区四区五区 | 在线不卡日韩 | 国产成人精品曰本亚洲77美色 | 最新国产午夜精品视频不卡 | 在线成人播放毛片 | 国产在线精品一区免费香蕉 | 国产精品日韩一区二区三区 | 性做久久久久久久 |