ThreadLocal---线程本地变量

 

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

For example, the class below generates unique identifiers local to each thread. A thread's id is assigned the first time it invokes UniqueThreadIdGenerator.getCurrentThreadId() and remains unchanged on subsequent calls.

 

先看两个例子:

 

1.

 

[java]  view plain copy print ?
  1. package com.mytrace.thread;  
  2.   
  3. public class TestThread {  
  4.     public static Integer a = 0;  
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         MyThread my1 = new MyThread();  
  10.         MyThread my2 = new MyThread();  
  11.         my1.start();  
  12.         my2.start();  
  13.         try {  
  14.             Thread.sleep(3000);  
  15.         } catch (InterruptedException e) {  
  16.             // TODO Auto-generated catch block  
  17.             e.printStackTrace();  
  18.         }  
  19.         System.out.println(a + "------");  
  20.     }  
  21.     public static class MyThread extends Thread {  
  22.         public void run() {  
  23. //          synchronized (a) {  
  24.                 for (int i = 0; i < 5; i++) {  
  25.                     a++;  
  26.                     System.out.println(Thread.currentThread().getName() + ":"  
  27.                     + a);  
  28.                 }  
  29. //          }  
  30.         }  
  31.     }  
  32. }  

 

结果:

Thread-0:2
Thread-0:3
Thread-1:2
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-0:4
Thread-0:9
Thread-0:10
10------

 

 

2.

 

[java]  view plain copy print ?
  1. package com.mytrace.thread;  
  2. public class TestThreadLocal2 {  
  3.     private static ThreadLocal<Integer> a = new ThreadLocal<Integer>() {  
  4.         public Integer initialValue() { // 初始化,默认是返回 null  
  5.             return 0;  
  6.         }  
  7.     };  
  8.     public static void main(String args[]) {  
  9.         MyThread my1 = new MyThread();  
  10.         MyThread my2 = new MyThread();  
  11.         my1.start();  
  12.         my2.start();  
  13.         try {  
  14.             Thread.sleep(3000);  
  15.         } catch (InterruptedException e) {  
  16.             // TODO Auto-generated catch block  
  17.             e.printStackTrace();  
  18.         }  
  19.         System.out.println(a.get() + "------");  
  20.     }  
  21.     public static class MyThread extends Thread {  
  22.         public void run() {  
  23.             for (int i = 0; i < 5; i++) {  
  24.                 a.set(a.get() + 1);  
  25.                 System.out.println(Thread.currentThread().getName() + ":"  
  26.                 + a.get());  
  27.             }  
  28.         }  
  29.     }  
  30. }  

 

结果:

Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
Thread-1:5
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
0------

 

从以上两个例子看出: ThreadLocal为每个线程创建一个ThreadLocal<Integer> a我副本,每个线程各自操作自己的那个副本, 不会影响其它线程的副本..


原文:http://blog.csdn.net/zsxxsz/article/details/6284769

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值