ThreadLocal是什么呢?其实
ThreadLocal不是一个线程的本地实现版本,也不是一个Thread。
<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">的目的就是为每一个使用</span><span style="color:#ff6600; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">的线程都提供一个值,<br style="line-height:25px"> 让该值和使用它的线程绑定,当然每一个线程都可以独立地改变它绑定的值<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 主要函数 <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="pubmethods" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Public Methods</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">T</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#get()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">get</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns the value of this variable for the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回当前线程的线程局部变量副本</div> </div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">void</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#remove()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">remove</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Removes the entry for this variable in the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 如果我们想把ThreadLocal所绑定的对象的引用清空,请不要粗暴的把ThreadLocal设置null,而应该调用remove()方法</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">void</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#set(T)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">set</a></span>(T value)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Sets the value of this variable for the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 设置当前线程的线程局部变量副本的值 </div> </td> </tr> </tbody></table> </div> <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="promethods" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Protected Methods</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">T</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#initialValue()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">initialValue</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Provides the initial value of this variable for the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 该方法返回当前线程在该线程局部变量的初始值。这个方法是一个延迟调用方法,在一个线程第1次调用get()且此时还没调用set(Object)时才执行,并且仅执行1次。ThreadLocal中返回的是null。该方法是一个protected的方法,它主要是为设置局部变量的初始值提供方便。</div> </td> </tr> </tbody></table> <br style="line-height:25px"> ThreadLocal是如何做到让每一个线程和一个值绑定的呢?<br style="line-height:25px"> 其实实现的思路很简单,在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本。<br style="line-height:25px"> 比如下面的示例实现:<br style="line-height:25px"> 示例1:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadLocal</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">Mapvalues=Collections.synchronizedMap(newHashMap());<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Objectget(){<br style="line-height:25px"> ThreadcurThread=Thread.currentThread();<br style="line-height:25px"> Objecto=values.get(curThread);<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(o==null&&!values.containsKey(curThread)){<br style="line-height:25px"> o=initialValue();<br style="line-height:25px"> values.put(curThread,o);<br style="line-height:25px"> }<br style="line-height:25px"> returno;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">set(ObjectnewValue){<br style="line-height:25px"> values.put(Thread.currentThread(),newValue);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span>Object<span style="color:#3366ff; line-height:25px">initialValue(){<br style="line-height:25px"> returnnull;<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">注意:</span><span style="color:#000080; line-height:25px">以上只是一个粗略的实现。我觉得上面的没有必要使用Collections.synchronizedMap。<br style="line-height:25px"> 因为不同的线程不可能对HashMap的同一项进行操作<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 在J<span style="line-height:25px"><wbr style="line-height:25px">DK中的ThreadLocal的实现</wbr></span><wbr style="line-height:25px">感觉很巧妙:<br style="line-height:25px"> 下面是去掉了注释后ThreadLocal的代码<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadLocal<T></span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatefinalint</span><span style="color:#ff6600; line-height:25px">threadLocalHashCode</span><span style="color:#3366ff; line-height:25px">=nextHashCode();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatestaticAtomicIntegernextHashCode=</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newAtomicInteger();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><wbr style="line-height:25px"><span style="line-height:25px; color:rgb(51,102,255)"></span><span style="color:#993300; line-height:25px">privatestaticfinalint</span><span style="color:#99cc00; line-height:25px">HASH_INCREMENT</span><span style="color:#0000ff; line-height:25px">=0x61c88647<wbr style="line-height:25px">;</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatestaticintnextHashCode(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnnextHashCode.getAndAdd(HASH_INCREMENT);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">protected</span><span style="color:#3366ff; line-height:25px">TinitialValue(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnnull;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">ThreadLocal(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicTget(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=Thread.currentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapmap=getMap(t);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(map!=null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMap.Entrye=map.getEntry(this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(e!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return(T)e.value;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnsetInitialValue();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateTsetInitialValue(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Tvalue=initialValue();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=Thread.currentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapmap=getMap(t);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(map!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">map.set(this,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">createMap(t,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnvalue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">set(Tvalue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=Thread.currentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapmap=getMap(t);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(map!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">map.set(this,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">createMap(t,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">remove(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapm=getMap(Thread.currentThread());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(m!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">m.remove(this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapgetMap(Threadt){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">t.threadLocals;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">voidcreateMap(Threadt,TfirstValue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t.threadLocals=newThreadLocalMap(this,firstValue);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticThreadLocalMapcreateInheritedMap(ThreadLocalMapparentMap){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnnewThreadLocalMap(parentMap);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">TchildValue(TparentValue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thrownewUnsupportedOperationException();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1:</wbr></span><span style="color:#000080; line-height:25px">每个线程有个</span><span style="color:#ff6600; line-height:25px">ThreadLocalMapthreadLocals</span><span style="color:#000080; line-height:25px">,它是把</span><span style="color:#ff9900; line-height:25px">ThreadLocal</span><span style="color:#000080; line-height:25px">作为</span><span style="color:#ff9900; line-height:25px">threadLocals</span><span style="color:#000080; line-height:25px">键值在使用的。这点比示例1的算法先进多。它没有了同步问题。因为它被创建后根本就没读写操作。</span><br style="line-height:25px"><span style="line-height:25px">注意2:</span><span style="color:#ff6600; line-height:25px">ThreadLocalMap</span><span style="color:#000080; line-height:25px">就在ThreadLocal中,但是它并没有使用HashMap,它使用的算法有复杂,没看明白</span><wbr style="line-height:25px">。<br style="line-height:25px"><span style="line-height:25px">注意3</span>:<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">如果我们对hashcode不是通过该对象的成员生成,而是使其自动生成时,且采用取余形式得到哈希值,增量(</span><wbr style="line-height:25px"><wbr style="line-height:25px"><span style="color:#99cc00; line-height:25px">HASH_INCREMENT</span><wbr style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">)最好是个</span><span style="color:#0000ff; line-height:25px">素数</span><span style="color:#000080; line-height:25px">。这样增量的才不能被任何才小于取余值(哈希表的长度)整除,否则会哈希到同一个桶中。</span><br style="line-height:25px"><span style="color:#ff6600; line-height:25px">ThreadLocalMap</span><span style="color:#000080; line-height:25px">中就是对hashcode取余的方式得到哈希值的。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">int</span><span style="color:#0000ff; line-height:25px">len=tab.length;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">int</span><span style="color:#0000ff; line-height:25px">i=key.hashCode&(len-1);</span><br style="line-height:25px"><span style="line-height:25px">因为</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *Theinitialcapacity--MUSTbeapoweroftwo.<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticfinalint</span><span style="color:#0000ff; line-height:25px">INITIAL_CAPACITY=16;</span><br style="line-height:25px"> 所以上面的"<span style="color:#0000ff; line-height:25px">inti=key.hashCode&(len-1)</span>;"相当于"<span style="color:#0000ff; line-height:25px">inti=key.hashCode%len;</span>"<br style="line-height:25px"> 虽然"<span style="color:#0000ff; line-height:25px">HASH_INCREMENT</span>"不是个素数,但是它却不能被tab.length(它等于2的n此方且大于等于16)被整除的。<wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4:</wbr></span><span style="color:#000080; line-height:25px">ThreadLocalMap中采用的是弱引用</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Entry</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">WeakReference<ThreadLocal>{<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">/**ThevalueassociatedwiththisThreadLocal.*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Objectvalue;</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Entry(ThreadLocalk,Objectv){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super(k);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">value=v;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:JDK为什么不使用hashMap或WeakHashMap,而是自己写了<wbr style="line-height:25px">ThreadLocalMap。应该主要是为提供了对不同key(这里是ThreadLocal)对象的但hashCode相同的存储的支持。<br style="line-height:25px"><span style="color:#003366; line-height:25px">如果希望线程局部变量初始化其它值,那么需要自己实现ThreadLocal的子类并重写</span><span style="color:#ff9900; line-height:25px">initialValue()</span><span style="color:#003366; line-height:25px">该方法,<br style="line-height:25px"> 通常使用一个内部匿名类对</span><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">进行子类化,比如下面的例子,SerialNum类为每一个类分配一个序号</span>:<br style="line-height:25px"> Java代码<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">SerialNum{:<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//Thenextserialnumbertobeassigned</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticint</span><span style="color:#3366ff; line-height:25px">nextSerialNum=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestatic</span><span style="color:#3366ff; line-height:25px">ThreadLocalserialNum=newThreadLocal(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">protectedsynchronized</span><span style="color:#3366ff; line-height:25px">ObjectinitialValue(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">returnnew</span><span style="color:#3366ff; line-height:25px">Integer(nextSerialNum++);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticint</span><span style="color:#3366ff; line-height:25px">get(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return((Integer)(serialNum.get())).intValue();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> SerialNum类的使用将非常地简单,因为get()方法是static的,所以在需要获取当前线程的序号时,简单地调用:<br style="line-height:25px"> Java代码<br style="line-height:25px"> i<span style="color:#0000ff; line-height:25px">ntserial=SerialNum.get();</span><br style="line-height:25px"> 即可。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">使用方法一</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Hibernate的文档中关于使ThreadLocal管理多线程访问的部分。<br style="line-height:25px"> 具体代码如下<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticfinal</span><span style="color:#3366ff; line-height:25px">ThreadLocalsession=newThreadLocal();<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">SessioncurrentSession(){<br style="line-height:25px"> Sessions=(Session)session.get();<br style="line-height:25px"> //openanewsession,ifthissessionhasnone<br style="line-height:25px"> if(s==null){<br style="line-height:25px"> s=sessionFactory.openSession();<br style="line-height:25px"> session.set(s);<br style="line-height:25px"> }<br style="line-height:25px"> returns;<br style="line-height:25px"> }</span><br style="line-height:25px"> 我们逐行分析<br style="line-height:25px"> 1。初始化一个ThreadLocal对象,ThreadLocal有三个成员方法get()、set()、initialvalue()。<br style="line-height:25px"> 如果不初始化initialvalue,则initialvalue返回null。<br style="line-height:25px"> 3。session的get根据当前线程返回其对应的线程内部变量,也就是我们需要的net.sf.hibernate.Session(相当于对应每个数据库连接).<br style="line-height:25px"> 多线程情况下共享数据库链接是不安全的。ThreadLocal保证了每个线程都有自己的s(数据库连接)。<br style="line-height:25px"> 5。如果是该线程初次访问,自然,s(数据库连接)会是null,接着创建一个Session,具体就是行6。<br style="line-height:25px"> 6。创建一个数据库连接实例s<br style="line-height:25px"> 7。保存该数据库连接s到ThreadLocal中。<br style="line-height:25px"> 8。如果当前线程已经访问过数据库了,则从session中get()就可以获取该线程上次获取过的连接实例。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">使用方法二</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 当要给线程初始化一个特殊值时,需要自己实现ThreadLocal的子类并重写该方法,<br style="line-height:25px"> 通常使用一个内部匿名类对ThreadLocal进行子类化,EasyDBO中创建jdbc连接上下文就是这样做的:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">JDBCContext</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">privatestatic</span><span style="color:#3366ff; line-height:25px">Loggerlogger=Logger.getLogger(JDBCContext.class);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateDataSourceds;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedConnectionconnection;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatebooleanisValid=true;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatestaticThreadLocaljdbcContext;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">JDBCContext(DataSourceds){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.ds=ds;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">createConnection();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">JDBCContextgetJdbcContext(javax.sql.DataSourceds)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(jdbcContext==null)jdbcContext=newJDBCContextThreadLocal(ds);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">JDBCContextcontext=(JDBCContext)jdbcContext.get();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(context==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">context=newJDBCContext(ds);</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> jdbcContext.set(</span><span style="line-height:25px; color:rgb(51,102,255)">context</span><span style="line-height:25px; color:rgb(51,102,255)">);</span> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">privatestaticclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">JDBCContextThreadLocal</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">ThreadLocal{<br style="line-height:25px"> publicjavax.sql.DataSourceds;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">JDBCContextThreadLocal(javax.sql.DataSourceds)<br style="line-height:25px"> {<br style="line-height:25px"> this.ds=ds;<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">protectedsynchronized</span><span style="color:#3366ff; line-height:25px">ObjectinitialValue(){<br style="line-height:25px"> returnnewJDBCContext(ds);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"> 使用单例模式,不同的线程调用getJdbcContext()获得自己的jdbcContext,<br style="line-height:25px"> 都是通过JDBCContextThreadLocal内置子类来获得JDBCContext对象的线程局部变量<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">总结</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">ThreadLocal和同步机制,两者面向的问题领域不同</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式;而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多个线程进行同步了。<br style="line-height:25px"> 所以,如果你需要进行多个线程之间进行通信,则使用同步机制;如果需要<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">隔离多个线程之间的共享冲突</span><wbr style="line-height:25px">,可以使用ThreadLocal,这将极大地简化你的程序,使程序更加易读、简洁。<br style="line-height:25px"></wbr></wbr></wbr></wbr> </div> <span style="color:#003366; line-height:25px">如果我们想把</span><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">所绑定的对象的引用清空,请不要粗暴的把</span><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">引用设置</span><span style="color:#0000ff; line-height:25px">nul</span><span style="color:#003366; line-height:25px">l,而应该调用remove()方法。否则会造成内存泄露</span>。关于此的更多内容请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011102411334093/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ThreadLocal的内存泄露</a></strong>》</wbr>
<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">的目的就是为每一个使用</span><span style="color:#ff6600; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">的线程都提供一个值,<br style="line-height:25px"> 让该值和使用它的线程绑定,当然每一个线程都可以独立地改变它绑定的值<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 主要函数 <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="pubmethods" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Public Methods</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">T</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#get()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">get</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns the value of this variable for the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回当前线程的线程局部变量副本</div> </div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">void</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#remove()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">remove</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Removes the entry for this variable in the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 如果我们想把ThreadLocal所绑定的对象的引用清空,请不要粗暴的把ThreadLocal设置null,而应该调用remove()方法</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">void</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#set(T)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">set</a></span>(T value)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Sets the value of this variable for the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 设置当前线程的线程局部变量副本的值 </div> </td> </tr> </tbody></table> </div> <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="promethods" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Protected Methods</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">T</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/lang/ThreadLocal.html#initialValue()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">initialValue</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Provides the initial value of this variable for the current thread.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 该方法返回当前线程在该线程局部变量的初始值。这个方法是一个延迟调用方法,在一个线程第1次调用get()且此时还没调用set(Object)时才执行,并且仅执行1次。ThreadLocal中返回的是null。该方法是一个protected的方法,它主要是为设置局部变量的初始值提供方便。</div> </td> </tr> </tbody></table> <br style="line-height:25px"> ThreadLocal是如何做到让每一个线程和一个值绑定的呢?<br style="line-height:25px"> 其实实现的思路很简单,在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本。<br style="line-height:25px"> 比如下面的示例实现:<br style="line-height:25px"> 示例1:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadLocal</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">Mapvalues=Collections.synchronizedMap(newHashMap());<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Objectget(){<br style="line-height:25px"> ThreadcurThread=Thread.currentThread();<br style="line-height:25px"> Objecto=values.get(curThread);<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(o==null&&!values.containsKey(curThread)){<br style="line-height:25px"> o=initialValue();<br style="line-height:25px"> values.put(curThread,o);<br style="line-height:25px"> }<br style="line-height:25px"> returno;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">set(ObjectnewValue){<br style="line-height:25px"> values.put(Thread.currentThread(),newValue);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span>Object<span style="color:#3366ff; line-height:25px">initialValue(){<br style="line-height:25px"> returnnull;<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">注意:</span><span style="color:#000080; line-height:25px">以上只是一个粗略的实现。我觉得上面的没有必要使用Collections.synchronizedMap。<br style="line-height:25px"> 因为不同的线程不可能对HashMap的同一项进行操作<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 在J<span style="line-height:25px"><wbr style="line-height:25px">DK中的ThreadLocal的实现</wbr></span><wbr style="line-height:25px">感觉很巧妙:<br style="line-height:25px"> 下面是去掉了注释后ThreadLocal的代码<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadLocal<T></span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatefinalint</span><span style="color:#ff6600; line-height:25px">threadLocalHashCode</span><span style="color:#3366ff; line-height:25px">=nextHashCode();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatestaticAtomicIntegernextHashCode=</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newAtomicInteger();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><wbr style="line-height:25px"><span style="line-height:25px; color:rgb(51,102,255)"></span><span style="color:#993300; line-height:25px">privatestaticfinalint</span><span style="color:#99cc00; line-height:25px">HASH_INCREMENT</span><span style="color:#0000ff; line-height:25px">=0x61c88647<wbr style="line-height:25px">;</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatestaticintnextHashCode(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnnextHashCode.getAndAdd(HASH_INCREMENT);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">protected</span><span style="color:#3366ff; line-height:25px">TinitialValue(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnnull;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">ThreadLocal(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicTget(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=Thread.currentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapmap=getMap(t);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(map!=null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMap.Entrye=map.getEntry(this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(e!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return(T)e.value;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnsetInitialValue();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateTsetInitialValue(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Tvalue=initialValue();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=Thread.currentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapmap=getMap(t);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(map!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">map.set(this,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">createMap(t,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnvalue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">set(Tvalue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=Thread.currentThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapmap=getMap(t);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(map!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">map.set(this,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">createMap(t,value);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">remove(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapm=getMap(Thread.currentThread());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(m!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">m.remove(this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadLocalMapgetMap(Threadt){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">t.threadLocals;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">voidcreateMap(Threadt,TfirstValue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t.threadLocals=newThreadLocalMap(this,firstValue);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticThreadLocalMapcreateInheritedMap(ThreadLocalMapparentMap){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnnewThreadLocalMap(parentMap);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">TchildValue(TparentValue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thrownewUnsupportedOperationException();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1:</wbr></span><span style="color:#000080; line-height:25px">每个线程有个</span><span style="color:#ff6600; line-height:25px">ThreadLocalMapthreadLocals</span><span style="color:#000080; line-height:25px">,它是把</span><span style="color:#ff9900; line-height:25px">ThreadLocal</span><span style="color:#000080; line-height:25px">作为</span><span style="color:#ff9900; line-height:25px">threadLocals</span><span style="color:#000080; line-height:25px">键值在使用的。这点比示例1的算法先进多。它没有了同步问题。因为它被创建后根本就没读写操作。</span><br style="line-height:25px"><span style="line-height:25px">注意2:</span><span style="color:#ff6600; line-height:25px">ThreadLocalMap</span><span style="color:#000080; line-height:25px">就在ThreadLocal中,但是它并没有使用HashMap,它使用的算法有复杂,没看明白</span><wbr style="line-height:25px">。<br style="line-height:25px"><span style="line-height:25px">注意3</span>:<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">如果我们对hashcode不是通过该对象的成员生成,而是使其自动生成时,且采用取余形式得到哈希值,增量(</span><wbr style="line-height:25px"><wbr style="line-height:25px"><span style="color:#99cc00; line-height:25px">HASH_INCREMENT</span><wbr style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">)最好是个</span><span style="color:#0000ff; line-height:25px">素数</span><span style="color:#000080; line-height:25px">。这样增量的才不能被任何才小于取余值(哈希表的长度)整除,否则会哈希到同一个桶中。</span><br style="line-height:25px"><span style="color:#ff6600; line-height:25px">ThreadLocalMap</span><span style="color:#000080; line-height:25px">中就是对hashcode取余的方式得到哈希值的。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">int</span><span style="color:#0000ff; line-height:25px">len=tab.length;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">int</span><span style="color:#0000ff; line-height:25px">i=key.hashCode&(len-1);</span><br style="line-height:25px"><span style="line-height:25px">因为</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *Theinitialcapacity--MUSTbeapoweroftwo.<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticfinalint</span><span style="color:#0000ff; line-height:25px">INITIAL_CAPACITY=16;</span><br style="line-height:25px"> 所以上面的"<span style="color:#0000ff; line-height:25px">inti=key.hashCode&(len-1)</span>;"相当于"<span style="color:#0000ff; line-height:25px">inti=key.hashCode%len;</span>"<br style="line-height:25px"> 虽然"<span style="color:#0000ff; line-height:25px">HASH_INCREMENT</span>"不是个素数,但是它却不能被tab.length(它等于2的n此方且大于等于16)被整除的。<wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4:</wbr></span><span style="color:#000080; line-height:25px">ThreadLocalMap中采用的是弱引用</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Entry</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">WeakReference<ThreadLocal>{<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">/**ThevalueassociatedwiththisThreadLocal.*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Objectvalue;</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Entry(ThreadLocalk,Objectv){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super(k);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">value=v;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:JDK为什么不使用hashMap或WeakHashMap,而是自己写了<wbr style="line-height:25px">ThreadLocalMap。应该主要是为提供了对不同key(这里是ThreadLocal)对象的但hashCode相同的存储的支持。<br style="line-height:25px"><span style="color:#003366; line-height:25px">如果希望线程局部变量初始化其它值,那么需要自己实现ThreadLocal的子类并重写</span><span style="color:#ff9900; line-height:25px">initialValue()</span><span style="color:#003366; line-height:25px">该方法,<br style="line-height:25px"> 通常使用一个内部匿名类对</span><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">进行子类化,比如下面的例子,SerialNum类为每一个类分配一个序号</span>:<br style="line-height:25px"> Java代码<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">SerialNum{:<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//Thenextserialnumbertobeassigned</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticint</span><span style="color:#3366ff; line-height:25px">nextSerialNum=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestatic</span><span style="color:#3366ff; line-height:25px">ThreadLocalserialNum=newThreadLocal(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">protectedsynchronized</span><span style="color:#3366ff; line-height:25px">ObjectinitialValue(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">returnnew</span><span style="color:#3366ff; line-height:25px">Integer(nextSerialNum++);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticint</span><span style="color:#3366ff; line-height:25px">get(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return((Integer)(serialNum.get())).intValue();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> SerialNum类的使用将非常地简单,因为get()方法是static的,所以在需要获取当前线程的序号时,简单地调用:<br style="line-height:25px"> Java代码<br style="line-height:25px"> i<span style="color:#0000ff; line-height:25px">ntserial=SerialNum.get();</span><br style="line-height:25px"> 即可。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">使用方法一</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Hibernate的文档中关于使ThreadLocal管理多线程访问的部分。<br style="line-height:25px"> 具体代码如下<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticfinal</span><span style="color:#3366ff; line-height:25px">ThreadLocalsession=newThreadLocal();<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">SessioncurrentSession(){<br style="line-height:25px"> Sessions=(Session)session.get();<br style="line-height:25px"> //openanewsession,ifthissessionhasnone<br style="line-height:25px"> if(s==null){<br style="line-height:25px"> s=sessionFactory.openSession();<br style="line-height:25px"> session.set(s);<br style="line-height:25px"> }<br style="line-height:25px"> returns;<br style="line-height:25px"> }</span><br style="line-height:25px"> 我们逐行分析<br style="line-height:25px"> 1。初始化一个ThreadLocal对象,ThreadLocal有三个成员方法get()、set()、initialvalue()。<br style="line-height:25px"> 如果不初始化initialvalue,则initialvalue返回null。<br style="line-height:25px"> 3。session的get根据当前线程返回其对应的线程内部变量,也就是我们需要的net.sf.hibernate.Session(相当于对应每个数据库连接).<br style="line-height:25px"> 多线程情况下共享数据库链接是不安全的。ThreadLocal保证了每个线程都有自己的s(数据库连接)。<br style="line-height:25px"> 5。如果是该线程初次访问,自然,s(数据库连接)会是null,接着创建一个Session,具体就是行6。<br style="line-height:25px"> 6。创建一个数据库连接实例s<br style="line-height:25px"> 7。保存该数据库连接s到ThreadLocal中。<br style="line-height:25px"> 8。如果当前线程已经访问过数据库了,则从session中get()就可以获取该线程上次获取过的连接实例。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">使用方法二</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 当要给线程初始化一个特殊值时,需要自己实现ThreadLocal的子类并重写该方法,<br style="line-height:25px"> 通常使用一个内部匿名类对ThreadLocal进行子类化,EasyDBO中创建jdbc连接上下文就是这样做的:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">JDBCContext</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">privatestatic</span><span style="color:#3366ff; line-height:25px">Loggerlogger=Logger.getLogger(JDBCContext.class);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateDataSourceds;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedConnectionconnection;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatebooleanisValid=true;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatestaticThreadLocaljdbcContext;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">JDBCContext(DataSourceds){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.ds=ds;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">createConnection();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">JDBCContextgetJdbcContext(javax.sql.DataSourceds)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(jdbcContext==null)jdbcContext=newJDBCContextThreadLocal(ds);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">JDBCContextcontext=(JDBCContext)jdbcContext.get();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(context==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">context=newJDBCContext(ds);</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> jdbcContext.set(</span><span style="line-height:25px; color:rgb(51,102,255)">context</span><span style="line-height:25px; color:rgb(51,102,255)">);</span> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">privatestaticclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">JDBCContextThreadLocal</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">ThreadLocal{<br style="line-height:25px"> publicjavax.sql.DataSourceds;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">JDBCContextThreadLocal(javax.sql.DataSourceds)<br style="line-height:25px"> {<br style="line-height:25px"> this.ds=ds;<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">protectedsynchronized</span><span style="color:#3366ff; line-height:25px">ObjectinitialValue(){<br style="line-height:25px"> returnnewJDBCContext(ds);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"> 使用单例模式,不同的线程调用getJdbcContext()获得自己的jdbcContext,<br style="line-height:25px"> 都是通过JDBCContextThreadLocal内置子类来获得JDBCContext对象的线程局部变量<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">总结</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">ThreadLocal和同步机制,两者面向的问题领域不同</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式;而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多个线程进行同步了。<br style="line-height:25px"> 所以,如果你需要进行多个线程之间进行通信,则使用同步机制;如果需要<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">隔离多个线程之间的共享冲突</span><wbr style="line-height:25px">,可以使用ThreadLocal,这将极大地简化你的程序,使程序更加易读、简洁。<br style="line-height:25px"></wbr></wbr></wbr></wbr> </div> <span style="color:#003366; line-height:25px">如果我们想把</span><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">所绑定的对象的引用清空,请不要粗暴的把</span><span style="color:#ff00ff; line-height:25px">ThreadLocal</span><span style="color:#003366; line-height:25px">引用设置</span><span style="color:#0000ff; line-height:25px">nul</span><span style="color:#003366; line-height:25px">l,而应该调用remove()方法。否则会造成内存泄露</span>。关于此的更多内容请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011102411334093/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ThreadLocal的内存泄露</a></strong>》</wbr>