ThreadLocal的使用,保证每一个线程都有自己的共享变量。

变量的共享可以使用public static 修饰,所有的线程都使用同一个public static 变量,那如何实现每一个线程都有自己的共享变量呢,ThradLocal类就是干这个用的。


[java]  view plain  copy
  1. package com.wupao.controller.test;  
  2.   
  3. public class ThreadLocalTest {  
  4.   
  5.     static ThreadLocal threadLocal =new ThreadLocal();  
  6.       
  7.       
  8.     public static  class ThreadA extends Thread{  
  9.         @Override  
  10.         public void run() {  
  11.             for (int i = 0; i < 20; i++) {  
  12.                 threadLocal.set("ThreadA-"+i);  
  13.                 System.out.println("ThreadA:"+threadLocal.get());  
  14.                 try {  
  15.                     Thread.sleep(100);  
  16.                 } catch (InterruptedException e) {  
  17.                     // TODO Auto-generated catch block  
  18.                     e.printStackTrace();  
  19.                 }  
  20.             }  
  21.           
  22.         }  
  23.     }  
  24.     public static  class ThreadB extends Thread{  
  25.         @Override  
  26.         public void run() {  
  27.             for (int i = 0; i < 20; i++) {  
  28.                 threadLocal.set("ThreadB-"+i);  
  29.             System.out.println("ThreadB:"+threadLocal.get());  
  30.             try {  
  31.                 Thread.sleep(100);  
  32.             } catch (InterruptedException e) {  
  33.                 // TODO Auto-generated catch block  
  34.                 e.printStackTrace();  
  35.             }  
  36.             }  
  37.           
  38.         }  
  39.     }  
  40.       
  41.     public static void main(String[] args) {  
  42.         ThreadA a =new ThreadA();  
  43.       
  44.         ThreadB b =new ThreadB();  
  45.         a.start();  
  46.         b.start();  
  47.     }  
  48.       
  49. }  

结果为:

ThreadB:ThreadB-0
ThreadA:ThreadA-0
ThreadB:ThreadB-1
ThreadA:ThreadA-1
ThreadB:ThreadB-2
ThreadA:ThreadA-2
ThreadB:ThreadB-3
ThreadA:ThreadA-3
ThreadB:ThreadB-4
ThreadA:ThreadA-4
ThreadB:ThreadB-5
ThreadA:ThreadA-5
ThreadA:ThreadA-6
ThreadB:ThreadB-6
ThreadB:ThreadB-7
ThreadA:ThreadA-7
ThreadA:ThreadA-8
ThreadB:ThreadB-8
ThreadA:ThreadA-9
ThreadB:ThreadB-9
ThreadB:ThreadB-10
ThreadA:ThreadA-10
ThreadB:ThreadB-11
ThreadA:ThreadA-11
ThreadA:ThreadA-12
ThreadB:ThreadB-12
ThreadB:ThreadB-13
ThreadA:ThreadA-13
ThreadA:ThreadA-14
ThreadB:ThreadB-14
ThreadB:ThreadB-15
ThreadA:ThreadA-15
ThreadA:ThreadA-16
ThreadB:ThreadB-16
ThreadA:ThreadA-17
ThreadB:ThreadB-17
ThreadB:ThreadB-18
ThreadA:ThreadA-18
ThreadA:ThreadA-19
ThreadB:ThreadB-19

虽然两个线程同时对threadlocal 进行存储,但是还是能分别get出属于自己线程的变量值。


底层原理:

[java]  view plain  copy
  1. public void set(T value) {  
  2.        Thread t = Thread.currentThread();  
  3.        ThreadLocalMap map = getMap(t);  
  4.        if (map != null)  
  5.            map.set(this, value);  
  6.        else  
  7.            createMap(t, value);  
  8.    }  

[java]  view plain  copy
  1. ThreadLocalMap getMap(Thread t) {  
  2.        return t.threadLocals;  
  3.    }  

当前的每个线程自带一个map,当前线程对象作为key存到map(具体是ThreadLocal.ThreadLocalMap)里,要存的真实value作为map的v。


使用场景:

可以为每个线程存储不需要共享的数据,比如链接或者session,又或者存储像SimpleDateFormat 这样创建费时间又线程不安全的数据。

了解更多内容:请参考 http://www.cnblogs.com/dolphin0520/p/3920407.html

转载于:https://blog.csdn.net/liuyifeng1920/article/details/53186116

你可能感兴趣的:(并发编程,--,多线程基础)