Java多线程之ThreadLocal

来源:互联网 发布:程序员教程pdf百度云 编辑:程序博客网 时间:2024/04/30 07:50

下面是ThreadLocal的测试代码,更多信息请参考注释

  1. package com.jadyer.thread.local;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /** 
  6.  * ThreadLocal Test 
  7.  * @see ============================================================================================================= 
  8.  * @see ThreadLocal的作用和目的 
  9.  * @see ThreadLocal用于实现线程内的数据共享。即对于相同的代码,多个模块在不同的线程中运行时,分别共享不同的数据 
  10.  * @see 每个线程调用全局的ThreadLocal.set()方法,就相当于在其内部的Map中增加一条记录,key是各自的线程,value是各自set()传进去的值 
  11.  * @see ============================================================================================================= 
  12.  * @see ThreadLocal的应用场景 
  13.  * @see 例如Struts2中的ActionContext,同一段代码被不同的线程调用运行时,该代码操作的数据是每个线程各自的状态和数据 
  14.  * @see 对于不同的线程来说,ActionContext.getContext()方法得到的对象都不相同 
  15.  * @see 对于同一个线程来说,ActionContext.getContext()方法无论在哪个模块中或者是被调用多少次,其得到的都是同一个对象 
  16.  * @see 通过查看com.opensymphony.xwork2.ActionContex的第43和166行源码,不难发现,Struts2就是这么做的 
  17.  * @see ============================================================================================================= 
  18.  * @see 线程中的成员变量和局部变量 
  19.  * @see 成员变量:多个线程操作同一个对象的成员变量时,它们对成员变量的改变是彼此影响的 
  20.  * @see 局部变量:每个线程都会有一个该局部变量的拷贝,一个线程对局部变量的改变不会影响到其它线程对该局部变量的操作 
  21.  * @see ============================================================================================================= 
  22.  * @author 宏宇 
  23.  * @create Feb 27, 2012 12:10:24 AM 
  24.  */  
  25. public class ThreadLocalTest {  
  26.     public static void main(String[] args) {  
  27.         new Thread(new MyThread(new Random().nextInt())).start();  
  28.         new Thread(new MyThread(new Random().nextInt())).start();  
  29.     }  
  30. }  
  31.   
  32.   
  33. class MyThread implements Runnable{  
  34.     private Integer data;  
  35.     public MyThread(Integer data){  
  36.         this.data = data;  
  37.     }  
  38.     @Override  
  39.     public void run() {  
  40.         System.out.println(Thread.currentThread().getName() + " has put data:" + data);  
  41.         User.getThreadInstance().setName("name" + data);  
  42.         User.getThreadInstance().setAge(data);  
  43.         new Pig().getMyData();  
  44.         new Dog().getMyData();  
  45.     }  
  46. }  
  47.   
  48.   
  49. class Pig{  
  50.     public void getMyData(){  
  51.         User user = User.getThreadInstance();  
  52.         System.out.println("Pig from " + Thread.currentThread().getName() + " getMyData:" + user.getName() + "|" + user.getAge());  
  53.     }  
  54. }  
  55.   
  56.   
  57. class Dog{  
  58.     public void getMyData(){  
  59.         User user = User.getThreadInstance();  
  60.         System.out.println("Dog from " + Thread.currentThread().getName() + " getMyData:" + user.getName() + "|" + user.getAge());  
  61.     }  
  62. }  
  63.   
  64.   
  65.   
  66.   
  67. /** 
  68.  * 自定义的线程范围内共享的对象。即该类会针对不同的线程分别创建一个独立的对象 
  69.  * @see 此时每个线程得到的将是自己的实例,各线程间得到的实例没有任何关联 
  70.  * @see 我们可以拿它,与单例模式中的懒汉式,进行对比,这是个很有意思的东西 
  71.  * @see Struts2就是这么设计的,它的每一个请求就是一个线程 
  72.  */  
  73. class User{  
  74.     private static ThreadLocal<User> instanceMap = new ThreadLocal<User>();  
  75.       
  76.     private User(){}  
  77.       
  78.     /** 
  79.      * 得到与当前线程相关的,当前类的实例 
  80.      */  
  81.     public static /*synchronized*/ User getThreadInstance(){  
  82.         User instance = instanceMap.get();  
  83.         if(null == instance){  
  84.             instance = new User();  
  85.             instanceMap.set(instance);  
  86.         }  
  87.         return instance;  
  88.     }  
  89.       
  90.     private String name;  
  91.     private int age;  
  92.     public String getName() {  
  93.         return name;  
  94.     }  
  95.     public void setName(String name) {  
  96.         this.name = name;  
  97.     }  
  98.     public int getAge() {  
  99.         return age;  
  100.     }  
  101.     public void setAge(int age) {  
  102.         this.age = age;  
  103.     }  
  104. }  
原创粉丝点击