ThreadLocal 那点事儿

论坛 期权论坛     
选择匿名的用户   2021-6-2 17:55   143   0
<p style="padding-top:0px; padding-bottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; letter-spacing:0.5px; font-size:13px; color:rgb(51,51,51); font-family:微软雅黑,Verdana,sans-serif,宋体"> ThreadLocal,直译为“线程本地”或“本地线程”,如果你真的这么认为,那就错了!其实,它就是一个容器,用于存放线程的局部变量,我认为应该叫做 ThreadLocalVariable(线程局部变量)才对,真不理解为什么当初 Sun 公司的工程师这样命名。</p>
<p style="padding-top:0px; padding-bottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; letter-spacing:0.5px; font-size:13px; color:rgb(51,51,51); font-family:微软雅黑,Verdana,sans-serif,宋体"> 早在 JDK 1.2 的时代,java.lang.ThreadLocal 就诞生了,它是为了解决多线程并发问题而设计的,只不过设计得有些难用,所以至今没有得到广泛使用。其实它还是挺有用的,不相信的话,我们一起来看看这个例子吧。</p>
<p style="padding-top:0px; padding-bottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; letter-spacing:0.5px; font-size:13px; color:rgb(51,51,51); font-family:微软雅黑,Verdana,sans-serif,宋体"> 一个序列号生成器的程序,可能同时会有多个线程并发访问它,要保证每个线程得到的序列号都是自增的,而不能相互干扰。</p>
<p style="padding-top:0px; padding-bottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; letter-spacing:0.5px; font-size:13px; color:rgb(51,51,51); font-family:微软雅黑,Verdana,sans-serif,宋体"> <span style="padding:0px; margin:0px; line-height:1.5; font-size:10pt">先定义一个接口:</span></p>
<div class="syntaxhighlighter" id="highlighter_704760" style="width:1097.4375px; font-size:13px; color:rgb(51,51,51); padding:1px!important; margin:1em 0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:relative!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; line-height:1.1em!important; font-family:Consolas,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,Courier,monospace!important; min-height:inherit!important">
<div class="lines" style="padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-size:10pt!important; min-height:inherit!important">
  <div class="line alt1" style="padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-size:10pt!important; min-height:inherit!important">
   <table style="padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-size:10pt!important; min-height:inherit!important; border-collapse:collapse!important"><tbody style="padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-size:10pt!important; min-height:inherit!important"><tr style="padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-size:10pt!important; min-height:inherit!important"><td class="number" style="padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:top!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:3em!important; line-height:1.1em!important; font-family:Consolas,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,Courier,monospace!important; font-size:10pt!important; min-height:inherit!important; color:rgb(175,175,175)!important"> <code style="word-break:break-all; padding:0px 0.3em 0px 0px!important; margin:0px!important; font-family:Consolas,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,Courier,monospace!important; border:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:2.7em!important; line-hei
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP