ThreadLocal 的使用

要想在多个类中访问同一个值,通常会用到静态成员。但是在多线程环境下,对静态成员就会出现你存我取的现象。如何保证静态成员不会被其他的线程访问呢?这就要用到 ThreadLocal。下面是两个例子:

[java]  view plain copy
  1. /** 
  2.  * 演示 ThreadLocal 的使用 
  3.  */  
  4. public class ThreadLocalTest {  
  5.    
  6.     // ThreadLocal 是一个泛型容器  
  7.     private static ThreadLocal<String> threadName = new ThreadLocal<String>();  
  8.    
  9.     // 将对象放入 ThreadLocal  
  10.     public static void setThreadName(String name) {  
  11.         threadName.set(name);  
  12.     }  
  13.    
  14.     // 从 ThreadLocal 中读取内容  
  15.     public static String getThreadName() {  
  16.         return threadName.get();  
  17.     }  
  18.    
  19.     // 程序入口  
  20.     public static void main(String[] args) throws Exception {  
  21.    
  22.         // 启动 5 个线程。它们隔一段时间输出一行文字。  
  23.         // 文字的内容是从同一个静态方法中取出来的,但  
  24.         // 这 5 个线程输出的内容各不相同。这是因为使用  
  25.         // 了 ThreadLocal。  
  26.         for (int i = 0; i < 5; i++) {  
  27.             new TestThread(i).start();  
  28.             Thread.sleep(500);  
  29.         }  
  30.     }  
  31.    
  32.     // 测试线程  
  33.     private static class TestThread extends Thread {  
  34.    
  35.         private int index;  
  36.    
  37.         // 注意这个方法的执行仍然是在主线程中  
  38.         public TestThread(int index) {  
  39.             this.index = index;  
  40.         }  
  41.    
  42.         // 这里才是在新的线程中执行  
  43.         @Override  
  44.         public void run() {  
  45.             ThreadLocalTest.setThreadName("Thread " + index);  
  46.    
  47.             while (true) {  
  48.                 System.out.println(ThreadLocalTest.getThreadName() + " ticks.");  
  49.                 try {  
  50.                     sleep(3000);  
  51.                 } catch (InterruptedException e) {  
  52.                     // nothing to do  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  
  57. }  

 

[java]  view plain copy
  1. /** 
  2.  * 演示 ThreadLocal 的使用 
  3.  */  
  4. public class ThreadLocalTest2 {  
  5.    
  6.     private String threadName;  
  7.    
  8.     private ThreadLocal<String> threadNameLocal = new ThreadLocal<String>();  
  9.    
  10.     public String getThreadName() {  
  11.         return threadName;  
  12.     }  
  13.    
  14.     public void setThreadName(String threadName) {  
  15.         this.threadName = threadName;  
  16.     }  
  17.    
  18.     public String getThreadNameLocal() {  
  19.         return threadNameLocal.get();  
  20.     }  
  21.    
  22.     public void setThreadNameLocal(String nameLocal) {  
  23.         threadNameLocal.set(nameLocal);  
  24.     }  
  25.    
  26.     // 程序入口  
  27.     public static void main(String[] args) throws Exception {  
  28.         final ThreadLocalTest2 testObject = new ThreadLocalTest2();  
  29.    
  30.         // 启用三个线程同时操作 testObject  
  31.         for (int i = 0; i < 3; i++) {  
  32.             final int n = i;  
  33.    
  34.             new Thread() {  
  35.                 private int id = n;  
  36.    
  37.                 @Override  
  38.                 public void run() {  
  39.                     try {  
  40.                         // 可以在输出中看到,每个线程在设置了 theadName 之后,再取出来时已经是一个不同的值;  
  41.                         // 而每个线程在设置了 theadNameLocal,再取出来还是原来的值。  
  42.    
  43.                         testObject.setThreadName(String.valueOf(this.hashCode()));  
  44.                         System.out.println("线程 " + id + " 设置 theadName 为 " + this.hashCode());  
  45.    
  46.                         testObject.setThreadNameLocal(String.valueOf(this.hashCode()));  
  47.                         System.out.println("线程 " + id + " 设置 theadNameLocal 为 " + this.hashCode());  
  48.    
  49.                         Thread.sleep(200);  
  50.    
  51.                         System.out.println("线程 " + id + " 获取 theadName 为 " + testObject.getThreadName());  
  52.                         System.out.println("线程 " + id + " 获取 theadNameLocal 为 " + testObject.getThreadNameLocal());  
  53.                     } catch (InterruptedException e) {  
  54.                         // nothing to do  
  55.                     }  
  56.                 }  
  57.             }.start();  
  58.    
  59.             Thread.sleep(100);  
  60.         }  
  61.     }  
  62. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值