A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 如梦初醒 中级黑马   /  2012-4-13 11:52  /  1330 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

<p>
        &nbsp;
</p>
<p>
        <img alt="" src="http://my.csdn.net/uploads/201204/13/1334288858_1539.png" />
</p>
<p>
        &nbsp;
</p>
<p>
        &nbsp;
</p>
<p>
        package cn.itcast.heima2;<br />
        import java.util.Random;<br />
        public class ThreadLocalTest {<br />
        &nbsp;private static ThreadLocal&lt;Integer&gt; x = new ThreadLocal&lt;Integer&gt;();<br />
        &nbsp;private static ThreadLocal&lt;MyThreadScopeData&gt; myThreadScopeData = new ThreadLocal&lt;MyThreadScopeData&gt;();<br />
        &nbsp;public static void main(String[] args) {<br />
        &nbsp;&nbsp;for(int i=0;i&lt;2;i++){<br />
        &nbsp;&nbsp;&nbsp;new Thread(new Runnable(){<br />
        &nbsp;&nbsp;&nbsp;&nbsp;@Override<br />
        &nbsp;&nbsp;&nbsp;&nbsp;public void run() {<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int data = new Random().nextInt();<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(Thread.currentThread().getName() <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot; has put data :&quot; + data);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.set(data);<br />
        /*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyThreadScopeData myData = new MyThreadScopeData();<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myData.setName(&quot;name&quot; + data);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myData.setAge(data);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myThreadScopeData.set(myData);*/<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyThreadScopeData.getThreadInstance().setName(&quot;name&quot; + data);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyThreadScopeData.getThreadInstance().setAge(data);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new A().get();<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new B().get();<br />
        &nbsp;&nbsp;&nbsp;&nbsp;}<br />
        &nbsp;&nbsp;&nbsp;}).start();<br />
        &nbsp;&nbsp;}<br />
        &nbsp;}<br />
        &nbsp;static class A{<br />
        &nbsp;&nbsp;public void get(){<br />
        &nbsp;&nbsp;&nbsp;int data = x.get();<br />
        &nbsp;&nbsp;&nbsp;System.out.println(&quot;A from &quot; + Thread.currentThread().getName() <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot; get data :&quot; + data);<br />
        /*&nbsp;&nbsp;&nbsp;MyThreadScopeData myData = myThreadScopeData.get();;<br />
        &nbsp;&nbsp;&nbsp;System.out.println(&quot;A from &quot; + Thread.currentThread().getName() <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot; getMyData: &quot; + myData.getName() + &quot;,&quot; +<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myData.getAge());*/<br />
        &nbsp;&nbsp;&nbsp;MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();<br />
        &nbsp;&nbsp;&nbsp;System.out.println(&quot;A from &quot; + Thread.currentThread().getName() <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot; getMyData: &quot; + myData.getName() + &quot;,&quot; +<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myData.getAge());<br />
        &nbsp;&nbsp;}<br />
        &nbsp;}<br />
        &nbsp;static class B{<br />
        &nbsp;&nbsp;public void get(){<br />
        &nbsp;&nbsp;&nbsp;int data = x.get();&nbsp;&nbsp;&nbsp;<br />
        &nbsp;&nbsp;&nbsp;System.out.println(&quot;B from &quot; + Thread.currentThread().getName() <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot; get data :&quot; + data);<br />
        &nbsp;&nbsp;&nbsp;MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();<br />
        &nbsp;&nbsp;&nbsp;System.out.println(&quot;B from &quot; + Thread.currentThread().getName() <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot; getMyData: &quot; + myData.getName() + &quot;,&quot; +<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myData.getAge());&nbsp;&nbsp;&nbsp;<br />
        &nbsp;&nbsp;}&nbsp;&nbsp;<br />
        &nbsp;}<br />
        }<br />
        class MyThreadScopeData{<br />
        &nbsp;private MyThreadScopeData(){}<br />
        &nbsp;public static /*synchronized*/ MyThreadScopeData getThreadInstance(){<br />
        &nbsp;&nbsp;MyThreadScopeData instance = map.get();<br />
        &nbsp;&nbsp;if(instance == null){<br />
        &nbsp;&nbsp;&nbsp;instance = new MyThreadScopeData();<br />
        &nbsp;&nbsp;&nbsp;map.set(instance);<br />
        &nbsp;&nbsp;}<br />
        &nbsp;&nbsp;return instance;<br />
        &nbsp;}<br />
        &nbsp;//private static MyThreadScopeData instance = null;//new MyThreadScopeData();<br />
        &nbsp;private static ThreadLocal&lt;MyThreadScopeData&gt; map = new ThreadLocal&lt;MyThreadScopeData&gt;();<br />
        &nbsp;<br />
        &nbsp;private String name;<br />
        &nbsp;private int age;<br />
        &nbsp;public String getName() {<br />
        &nbsp;&nbsp;return name;<br />
        &nbsp;}<br />
        &nbsp;public void setName(String name) {<br />
        &nbsp;&nbsp;this.name = name;<br />
        &nbsp;}<br />
        &nbsp;public int getAge() {<br />
        &nbsp;&nbsp;return age;<br />
        &nbsp;}<br />
        &nbsp;public void setAge(int age) {<br />
        &nbsp;&nbsp;this.age = age;<br />
        &nbsp;}<br />
        }<br />
       
</p>

1 个回复

正序浏览
怎么疑问呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马