日出星辰

          線程學(xué)習(xí)筆記【5】--ThreadLocal應(yīng)用

          基本的ThreadLocal使用

          public class ThreadLocalTest {

          static ThreadLocal tl=new ThreadLocal();
          public static void main(String[] args) {

          for(int i=0;i<2;i++){
          new Thread(new Runnable(){
          int data =new Random().nextInt();
          public void run() {
          System.out.println(Thread.currentThread().getName()
          +"存入的數(shù)據(jù)是 "+data);
          tl.set(data);
          //存到了當(dāng)前線程
          new A().getThreadData();
          }
          }).start();
          }
          }
          static class A{ //靜態(tài)類相當(dāng)于一個(gè)外部類
          public void getThreadData(){
          System.out.println(
          "data is "+tl.get());
          }
          }
          }

          結(jié)果可能是

          Thread-0存入的數(shù)據(jù)是 1997234255
          Thread-1存入的數(shù)據(jù)是 267171693
          data is 1997234255
          data is 267171693

          通過包裝對(duì)象非常爛的使用方式

          class MyThreadScopeData{
          private String name;
          private int age;
          public String getName() {
          return name;
          }
          public void setName(String name) {
          this.name = name;
          }
          public int getAge() {
          return age;
          }
          public void setAge(int age) {
          this.age = age;
          }
          }

           

          public class ThreadLocalTest {
          static ThreadLocal<MyThreadScopeData> myThreadScopeData=
          new ThreadLocal<MyThreadScopeData>();
          public static void main(String[] args) {

          for(int i=0;i<2;i++){
          new Thread(new Runnable(){
          int data =new Random().nextInt();
          public void run() {
          MyThreadScopeData mydata
          =new MyThreadScopeData();
          mydata.setName(
          "name is name"+data);
          mydata.setAge(data);
          //把對(duì)象存入ThreadLocal 這樣的做法非常爛!!!!!
          myThreadScopeData.set(mydata);
                            new B().showThreadScopeData();

          }
          }).start();
          }
          }


          static class B{
          public void showThreadScopeData(){
          System.out.println(myThreadScopeData.get().getName());
          System.out.println(
          "age is "+myThreadScopeData.get().getAge());
          }
          }

          }

          }

          標(biāo)準(zhǔn)使用方式

          /**
          * 單列線程
          * 在線程中范圍內(nèi)任意地方調(diào),得到都是同一個(gè)實(shí)例對(duì)象
          * 把ThreadLocal封裝到單列的內(nèi)部
          */
          class ThreadSingle{
          private ThreadSingle(){}
          public static ThreadLocal<ThreadSingle> map=new ThreadLocal<ThreadSingle>();
          //不需要加synchronized,即便有第2個(gè)線程進(jìn)入,但拿到的map.get()是獨(dú)有的。
          public static ThreadSingle getThreadInstance(){ //方法得到是與本線程相關(guān)的實(shí)例
          ThreadSingle obj=map.get();
          /**
          * 如果A進(jìn)入時(shí)obj=null,剛創(chuàng)建完還沒賦值,此時(shí)B線程進(jìn)入,但B和A沒有關(guān)系。
          */
          if(obj==null){
          obj
          =new ThreadSingle();
          map.set(obj);
          }
          return obj;
          }
          private String name;
          private int age;
          public String getName() {
          return name;
          }
          public void setName(String name) {
          this.name = name;
          }
          public int getAge() {
          return age;
          }
          public void setAge(int age) {
          this.age = age;
          }

          }

           

           

          public class ThreadLocalTest {
          
          	public static void main(String[] args) {
          
          		for(int i=0;i<2;i++){
          			new Thread(new Runnable(){
          				int data =new Random().nextInt();
          				public void run() {						
          					ThreadSingle.getThreadInstance().setName("name"+data);
          					ThreadSingle.getThreadInstance().setAge(data);
          					new C().showData();
          				}
          			}).start();
          		}
          	}
          

           

           

           

           

           

           

           

          posted on 2011-09-05 15:31 日出星辰 閱讀(131) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 德州市| 高阳县| 平山县| 土默特右旗| 当涂县| 东乌珠穆沁旗| 凤庆县| 万年县| 巩留县| 海阳市| 双峰县| 巴青县| 怀柔区| 天镇县| 繁昌县| 穆棱市| 辽宁省| 桃园县| 湟源县| 个旧市| 灵川县| 湖州市| 华亭县| 柳州市| 扎兰屯市| 泾源县| 通城县| 天津市| 康定县| 建德市| 日土县| 青浦区| 浦县| 沙雅县| 搜索| 福州市| 江西省| 宁都县| 锡林郭勒盟| 靖西县| 秭归县|