www.hj8828.comJava并发ThreadLocal类

算法 ww9992019com 发表于 1 周前

www.hj8828.com开户请拨15894452155世界人口将达97亿青岛四方区WfPt6j<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="10062">
                <input type="hidden" name="catid" id="catid" value="0">

                <p><code>ThreadLocal</code>类用于创建只能由同一个线程读取和写入的线程局部变量。 例如,如果两个线程正在访问引用相同<code>threadLocal</code>变量的代码,那么每个线程都不会看到任何其他线程操作完成的线程变量。</p>

<h2 id="h2-u7EBFu7A0Bu65B9u6CD5"><a name="%E7%BA%BF%E7%A8%8B%E6%96%B9%E6%B3%95" class="reference-link"></a><span class="header-link octicon octicon-link"></span>线程方法</h2><p>以下是<code>ThreadLocal</code>类中可用的重要方法的列表。</p>

<table>

<thead>

<tr>

<th>编号</th>

<th>方法</th>

<th>描述</th>

</tr>

</thead>

<tbody>

<tr>

<td>1</td>

<td><code>public T get()</code></td>

<td>返回当前线程的线程局部变量的副本中的值。</td>

</tr>

<tr>

<td>2</td>

<td><code>protected T initialValue()</code></td>

<td>返回此线程局部变量的当前线程的“初始值”。</td>

</tr>

<tr>

<td>3</td>

<td><code>public void remove()</code></td>

<td>删除此线程局部变量的当前线程的值。</td>

</tr>

<tr>

<td>4</td>

<td><code>public void set(T value)</code></td>

<td>将当前线程的线程局部变量的副本设置为指定的值。</td>

</tr>

</tbody>

</table>

<h2 id="h2-u5B9Eu4F8B"><a name="%E5%AE%9E%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>实例</h2><p>以下<code>TestThread</code>程序演示了<code>ThreadLocal</code>类的上面一些方法。 这里我们使用了两个计数器(<code>counter</code>)变量,一个是常量变量,另一个是<code>ThreadLocal</code>变量。</p>

<pre><code class="lang-java">class RunnableDemo implements Runnable {

int counter;

ThreadLocal&lt;Integer&gt; threadLocalCounter = new ThreadLocal&lt;Integer&gt;();

public void run() {

  counter++;

  if(threadLocalCounter.get() != null){

     threadLocalCounter.set(threadLocalCounter.get().intValue() + 1);

  }else{

     threadLocalCounter.set(0);

  }

  System.out.println("Counter: " + counter);

  System.out.println("threadLocalCounter: " + threadLocalCounter.get());

}

}

public class TestThread {

public static void main(String args[]) {

  RunnableDemo commonInstance = new RunnableDemo();



  Thread t1 = new Thread( commonInstance);

  Thread t2 = new Thread( commonInstance);

  Thread t3 = new Thread( commonInstance);

  Thread t4 = new Thread( commonInstance);



  t1.start();

  t2.start();

  t3.start();

  t4.start();



  // wait for threads to end

  try {

     t1.join();

     t2.join();

     t3.join();

     t4.join();

  }catch( Exception e) {

     System.out.println("Interrupted");

  }

}

}

</code></pre>

<p>这将产生以下结果 - </p>

<pre><code>Counter: 1

threadLocalCounter: 0

Counter: 2

threadLocalCounter: 0

Counter: 4

Counter: 3

threadLocalCounter: 0

threadLocalCounter: 0

</code></pre><p>您可以看到变量(<code>counter</code>)的值由每个线程增加,但是<code>ThreadLocalCounter</code>对于每个线程都保持为<code>0</code>。</p>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Java并发死锁"><i class="layui-icon"></i>
                            Java并发死锁</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Java并发Lock接口"><i class="layui-icon"></i>
                            Java并发Lock接口</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复