现在的位置: 首页 > 综合 > 正文

(5)ThreadLocal类及应用技巧—- 线程范围内共享变量

2013年12月16日 ⁄ 综合 ⁄ 共 8231字 ⁄ 字号 评论关闭

java.lang 
类 ThreadLocal<T>


java.lang.Object
  java.lang.ThreadLocal<T>
直接已知子类: 
InheritableThreadLocal 

public class ThreadLocal<T>extends Object该类提供了线程局部 (thread-local) 变量。


这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的每个线程都有自己的局部变量,它独立于变量的初始化副本。

ThreadLocal 实例通常是类中的 private static 字段,

它们希望将状态与某一个线程(例如,用户 ID 或事务 ID)相关联。 

例如,以下类生成对每个线程唯一的局部标识符。线程 ID 是在第一次调用 UniqueThreadIdGenerator.getCurrentThreadId() 时分配的,在后续调用中不会更改。 

  

import java.util.concurrent.atomic.AtomicInteger;


 public class UniqueThreadIdGenerator {


     private static final AtomicInteger uniqueId = new AtomicInteger(0);


     private static final ThreadLocal < Integer > uniqueNum = 
         new ThreadLocal < Integer > () {
             @Override protected Integer initialValue() {
                 return uniqueId.getAndIncrement();
         }
     };
 
     public static int getCurrentThreadId() {
         return uniqueId.get();
     }
 } // UniqueThreadIdGenerator

 每个线程都保持对其线程局部变量副本的隐式引用,只要线程是活动的并且 ThreadLocal 实例是可访问的;

在线程消失之后,其线程局部实例的所有副本都会被垃圾回收(除非存在对这些副本的其他引用)。 

ThreadfLocal

         ThreadfLocal是一种机制,很多人都说它的名字叫ThreadfLocalVariable更贴切。表面上看它和其他的java类没有区别,但是当一个多线程系统要求各个线程都有自己线程内的全局变量时,使用ThreadfLocal就是一个极佳的选择。

        ThreadfLocal用于实现线程内的数据共享,即对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,而在另外线程中运行时又共享另外一份数据。举个例子,我们在学校食堂排队打饭,对于甲同学打饭的全过程(线程一),那几个打饭、打菜和打汤的模块操作的(饭盆)变量是甲同学拿的饭盆;对于乙同学打饭的全过程(线程二),那几个打饭、打菜和打汤的模块操作的(饭盆)变量是乙同学拿的饭盆;在这个例子里,饭盆就可以视为应当存放到ThreadLocal里的共享变量。

 

         可能有人提到全局共享变量会想到用static变量,但是static变量的全局范围比ThreadfLocal里的更“全局”--在整个运行的jvm里只有一份,即虽然static变量可以被多个模块共享,但同时也是在多线程范围内共享。所以,用static是不能满足我们的业务需求的。当然,ThreadfLocal变量为static没有问题。

 

              ThreadLocal的一个常用业务场景是:在订单处理业务流中,减少库存量、增加一条流水台账、修改总账,这几个操作要在同一个事务中完成,通常也即同一个线程中进行处理,如果累加公司应收款的操作失败了,则应该把前面的操作回滚,否则,提交所有操作,这要求这些操作使用相同的数据库连接对象,而这些操作的代码分别位于不同的模块类中。

 

Thread与同步机制的比较


ThreadLocal和同步机制都是为了解决多线程中对相同变量的访问冲突的问题。

 

同步机制是通过锁对象机制保证同一时间只有一个线程访问变量。这时该变量是多个线程共享的,使用同步机制要求程序慎密地分析什么时候对变量进行读写,什么时候需要锁定某个对象,什么时候释放对象锁等繁杂的问题,程序设计和编写难度相对较大。

 

而ThreadLocal则从另一个角度来解决多线程的并发访问。ThreadLocal会为每一个线程提供一个独立的变量副本,从而隔离了多个线程对数据的访问冲突。因为每一个线程都拥有自己的变量副本,从而也就没有必要对该变量进行同步了。ThreadLocal提供了线程安全的共享对象,在编写多线程代码时,可以把不安全的变量封装进ThreadLocal。

 

概括起来说,对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而ThreadLocal采用了“以空间换时间”的方式。前者仅提供一份变量,让不同的线程排队访问,而后者为每一个线程都提供了一份变量,因此可以同时访问而互不影响。

 

不过,ThreadLocal并不能替代同步机制,两者面向的问题领域不同:ThreadLocal考虑的是变量在线程间的隔离;同步机制则提供了线程间通信的桥梁。

package com.itm.thread;

import java.util.Random;
/************
 * 
 * 利用 ThreadLocal 。
 * 
 * @author 
 *
 */
public class ThreadLocalTest {

	private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();// 泛型。
	
	
//	private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();
		
	public static void main(String[] args) {
		for(int i=0;i<2;i++){ // 用来创建两个线程。
			new	Thread( new Runnable(){ // 创建好一个线程。
				@Override
				public void run() {
					int data = new Random().nextInt(); // 准备好数据。
					System.out.println(Thread.currentThread().getName() + 
							"  has put data :" + data);
					
					x.set(data);// 向当前线程里面放入一个数据。把数据 存在当前线程里面去了。。。
					
					
					/***********
					 * 
					 * 多个变量。
					 * 
					 * 存数据。
					 * 
					 */
					/*MyThreadScopeData myData =  new MyThreadScopeData(); // new 一个这样的对象。
					myData.setName("name " + data);
					myData.setAge(data);
					myThreadScopeData.set(myData); // 存数据。。。
*/					
					new A().get(); // A 去取数据。
					new B().get();
				}
			}).start();
		}
	}
	
	static class A{
		public void get(){
			int data = x.get();// 取出  存入当前线程里面的数据。。。
			System.out.println("A form " + Thread.currentThread().getName() + "  get data :" + data);
			
			
			/*MyThreadScopeData myData = myThreadScopeData.get();
			System.out.println("A form " + Thread.currentThread().getName() + 
					" getMyData : " + 
						myData.getName() + "," + myData.getAge());
			*/
			
		}
	}
	
	static class B{
		public void get(){
			int data = x.get();
			System.out.println("B form " + Thread.currentThread().getName() + "  get data :" + data);
		}
	}

}

/*class MyThreadScopeData {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}*/

结果:

Thread-0  has put data :1365978062
A form Thread-0  get data :1365978062
Thread-1  has put data :-770297261
A form Thread-1  get data :-770297261
B form Thread-1  get data :-770297261
B form Thread-0  get data :1365978062

如果 线程范围内 有  N个变量  那应该怎么办呢??一个 threadLocal代表一个变量,

故其中里只能放一个数据。你有两个变量都要线程范围内共享,则要定义两个threadLocal对象,

如果有一百个变量要线程共享呢?那请先定义一个对象来装这一百个变量,然后在threadLocal中存储这个对象。


package com.itm.thread;

import java.util.Random;
/************
 * 
 * 利用 ThreadLocal 。
 * 
 * @author 
 *
 */
public class ThreadLocalTest {

	private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();// 泛型。
	
	
	private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();
		
	public static void main(String[] args) {
		for(int i=0;i<2;i++){ // 用来创建两个线程。
			new	Thread( new Runnable(){ // 创建好一个线程。
				@Override
				public void run() {
					int data = new Random().nextInt(); // 准备好数据。
					System.out.println(Thread.currentThread().getName() + 
							"  has put data :" + data);
					
					x.set(data);// 向当前线程里面放入一个数据。把数据 存在当前线程里面去了。。。
					
					
					/***********
					 * 
					 * 多个变量。
					 * 
					 * 存数据。
					 * 
					 */
					MyThreadScopeData myData =  new MyThreadScopeData(); // new 一个这样的对象。
					myData.setName("name " + data);
					myData.setAge(data);
					myThreadScopeData.set(myData); // 存数据。。。
					
					new A().get(); // A 去取数据。
					new B().get();
				}
			}).start();
		}
	}
	
	static class A{
		public void get(){
			int data = x.get();// 取出  存入当前线程里面的数据。。。
			System.out.println("A form " + Thread.currentThread().getName() + "  get data :" + data);
			
			
			MyThreadScopeData myData = myThreadScopeData.get();
			System.out.println("A form " + Thread.currentThread().getName() + 
					" getMyData : " + 
						myData.getName() + "," + myData.getAge());
			
			
		}
	}
	
	static class B{
		public void get(){
			int data = x.get();
			System.out.println("B form " + Thread.currentThread().getName() + "  get data :" + data);
		}
	}

}

class MyThreadScopeData {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

Thread-0  has put data :-485439611
Thread-1  has put data :198643933
A form Thread-0  get data :-485439611
A form Thread-0 getMyData : name -485439611,-485439611
A form Thread-1  get data :198643933
A form Thread-1 getMyData : name 198643933,198643933
B form Thread-1  get data :198643933
B form Thread-0  get data :-485439611



一中 更加优雅的写法:


package com.itm.thread;

import java.util.Random;
/************
 * 
 * 利用 ThreadLocal 。自己线程范围内的对象。
 * 
 * @author 
 *
 */
public class CopyOfThreadLocalTest2 {

	private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
	
	private static ThreadLocal<MyThreadScopeDataTwo> myThreadScopeDataTwo = new ThreadLocal<MyThreadScopeDataTwo>();
		
	public static void main(String[] args) {
		for(int i=0;i<2;i++){ // 用来创建两个线程。
			new	Thread( new Runnable(){ // 创建好一个线程。
				@Override
				public void run() {
					int data = new Random().nextInt(); // 准备好数据。
					System.out.println(Thread.currentThread().getName() + 
							"  has put data :" + data);
					
					x.set(data);// 向当前线程里面放入一个数据。
					
					
					
					/*MyThreadScopeDataTwo myData =  new MyThreadScopeDataTwo();
					myData.setName("name " + data);
					myData.setAge(data);
					myThreadScopeDataTwo.set(myData);*/
					
					// MyThreadScopeDataTwo.getThreadInstance() 拿到与 本线程相关的实例。
					MyThreadScopeDataTwo.getThreadInstance().setName("name " + data);
					MyThreadScopeDataTwo.getThreadInstance().setAge(data);
					
					new A().get(); // A 去取数据。
					new B().get();
				}
			}).start();
		}
	}
	
	static class A{
		public void get(){
			int data = x.get();
			System.out.println("A form " + Thread.currentThread().getName() + "  get data :" + data);
			
			MyThreadScopeDataTwo myData = MyThreadScopeDataTwo.getThreadInstance();
			
			System.out.println("A form " + Thread.currentThread().getName() + 
					" getMyData : " + 
						myData.getName() + "," + myData.getAge());
			
			
		}
	}
	
	static class B{
		public void get(){
			int data = x.get();
			System.out.println("B form " + Thread.currentThread().getName() + "  get data :" + data);
			
			MyThreadScopeDataTwo myData = MyThreadScopeDataTwo.getThreadInstance();
			
			System.out.println("A form " + Thread.currentThread().getName() + 
					" getMyData : " + 
						myData.getName() + "," + myData.getAge());
		}
	}

}

class MyThreadScopeDataTwo {
	
	/*
	 * 
	  
		  饱汉式。:人家还没有调用我  我就把这个对象创建出来了。
		 	private  MyThreadScopeDataTwo(){}
			public static MyThreadScopeDataTwo getInstance(){
				return instance;
			}
			private static MyThreadScopeDataTwo instance = new MyThreadScopeDataTwo();
		
		
	*/
	
	/*
	 
	 		饿汉式:
			private  MyThreadScopeDataTwo(){}
			// 加上 synchronized 时,这个对象就只能创建一个了。
			public static synchronized MyThreadScopeDataTwo getInstance(){
				if(instance == null){
					instance = new MyThreadScopeDataTwo();
				}
				return instance;
			}
			private static MyThreadScopeDataTwo instance = null;
	
	*/
	
	
	private  MyThreadScopeDataTwo(){}
	public static /*synchronized*/ MyThreadScopeDataTwo getThreadInstance(){
		MyThreadScopeDataTwo instance = map.get();
		if(instance == null){ // 不用加上  synchronized,两个线程各是各的实例。
			instance = new MyThreadScopeDataTwo();
			map.set(instance); // 把对象 放进去。。
		}
		return instance;
	}
	
	// 用来专门装对象的。。。
	private static ThreadLocal<MyThreadScopeDataTwo> map = new ThreadLocal<MyThreadScopeDataTwo>();
	
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

更多  threadLocal 借鉴:

http://blog.csdn.net/qjyong/article/details/2158097

http://www.iteye.com/topic/103804

抱歉!评论已关闭.