java ThreadLocal 用法

ThreadLocal这个类,理解起来比较简单,但是使用时,还是需要一些技巧的,简单介绍如下:

 

例子1:threadLocal变量一般用法

 

Java代码   收藏代码
  1. package concurrent.thread;  
  2.   
  3. /** 
  4.  * 技巧: 
  5.  *      1.在当前线程里执行threadLocal.set(value) 相当于 currentThread.threadLocalMap.set(threadLocal,value)。 
  6.  *          即:每个线程均含有一个threadLocalMap变量,该变量由ThreadLocal维护。 
  7.  *      2.ThreadLocal变量一般使用private static修饰。 
  8.  */  
  9. public class ThreadLocalDemo_step0 {  
  10.   
  11.     private static ThreadLocal<String> myData = new ThreadLocal<String>();  
  12.   
  13.     public static void main(String[] args) {  
  14.         Thread t1 = new Thread(new Runnable(){  
  15.             public void run() {  
  16.                 System.out.println("Thread " + Thread.currentThread().getName() + "[begin] has " + myData.get());  
  17.                 myData.set("t1");  
  18.                 System.out.println("Thread " + Thread.currentThread().getName() + "[end] has " + myData.get());  
  19.             }  
  20.         });  
  21.         Thread t2 = new Thread(new Runnable(){  
  22.             public void run() {  
  23.                 System.out.println("Thread " + Thread.currentThread().getName() + "[begin] has " + myData.get());  
  24.                 myData.set("t2");  
  25.                 System.out.println("Thread " + Thread.currentThread().getName() + "[end] has " + myData.get());  
  26.             }  
  27.         });  
  28.         t1.start();  
  29.         t2.start();  
  30.     }  
  31. }  
 

 

例子2:threadLocal变量的简单封装用法(重要)

 

Java代码   收藏代码
  1. package concurrent.thread;  
  2.   
  3. /** 
  4.  * 技巧: 
  5.  *      3.如果希望线程局部变量初始化其它值,那么需要自己实现ThreadLocal的子类并重写initialValue()方法, 
  6.  *      通常使用一个内部匿名类对ThreadLocal进行子类化.比如下面的例子,SerialNum类为每一个类分配一个序号: 
  7.  */  
  8. public class ThreadLocalDemo_step1 {  
  9.   
  10.     public static void main(String[] args) {  
  11.         Thread t1 = new Thread(new Runnable(){  
  12.             public void run() {  
  13.                 System.out.println("Thread " + Thread.currentThread().getName() + " has " + SerialNum.get());  
  14.             }  
  15.         });  
  16.         Thread t2 = new Thread(new Runnable(){  
  17.             public void run() {  
  18.                 System.out.println("Thread " + Thread.currentThread().getName() + " has " + SerialNum.get());  
  19.             }  
  20.         });  
  21.         t1.start();  
  22.         t2.start();  
  23.     }  
  24.       
  25.     //技巧:对threadLocal变量进行简单封装,可以简化多线程操作  
  26.     static class SerialNum {     
  27.          // The next serial number to be assigned     
  28.          private static int nextSerialNum = 0;     
  29.           
  30.          private static ThreadLocal serialNum = new ThreadLocal() {     
  31.              protected synchronized Object initialValue() {     
  32.                  return new Integer(nextSerialNum++);     
  33.              }     
  34.          };     
  35.           
  36.          public static int get() {     
  37.              return ((Integer) (serialNum.get())).intValue();     
  38.          }     
  39.      }     
  40.   
  41.       
  42. }  
 

参考文献:

 

http://my.oschina.net/ITBoy/blog/17190 java类ThreadLocal的理解

http://www.iteye.com/topic/103804 正确理解ThreadLocal

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值