模拟ThreadLocal类实现:线程范围内的共享变量,每一个线程只能访问他本身的,不能访问别的线程。java
package com.ljq.test.thread; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * 线程范围内的共享变量 * * 三个模块共享数据,主线程模块和AB模块 * * @author Administrator * */ public class ThreadScopeShareData { // 准备共享的数据 private static int data = 0; // 存放各个线程对应的数据 private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>(); 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(); // 将线程信息和对应数据存储起来 threadData.put(Thread.currentThread(), data); System.out.println(Thread.currentThread().getName() + " has put data :" + data); new A().get(); new B().get(); } }).start(); } } static class A { public void get() { int data = threadData.get(Thread.currentThread()); System.out.println("A from " + Thread.currentThread().getName() + " get data :" + data); } } static class B { public void get() { int data = threadData.get(Thread.currentThread()); System.out.println("B from " + Thread.currentThread().getName() + " get data :" + data); } } }
运行结果:数据库
ThreadLocal的做用和目的:
用于实现线程内的数据共享,即对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,而在另外线程中运行时又共享另一份数据。dom
每一个线程调用全局ThreadLocal对象的set方法,就至关于往其内部的map中增长一条记录,key分别是各自的线程,value是各自的set方法传进去的值。在线程结束时能够调用ThreadLocal.clear()方法,这样会更快释放内存,不调用也能够,由于线程结束后也能够自动释放相关的ThreadLocal变量。ide
ThreadLocal的应用场景:
订单处理包含一系列操做:减小库存量、增长一条流水台帐、修改总帐,这几个操做要在同一个事务中完成,一般也即同一个线程中进行处理,若是累加公司应收款的操做失败了,则应该把前面的操做回滚,不然,提交全部操做,这要求这些操做使用相同的数据库链接对象,而这些操做的代码分别位于不一样的模块类中。this
银行转帐包含一系列操做: 把转出账户的余额减小,把转入账户的余额增长,这两个操做要在同一个事务中完成,它们必须使用相同的数据库链接对象,转入和转出操做的代码分别是两个不一样的账户对象的方法。spa
例如Strut2的ActionContext,同一段代码被不一样的线程调用运行时,该代码操做的数据是每一个线程各自的状态和数据,对于不一样的线程来讲,getContext方法拿到的对象都不相同,对同一个线程来讲,无论调用getContext方法多少次和在哪一个模块中getContext方法,拿到的都是同一个。线程
实验案例:定义一个全局共享的ThreadLocal变量,而后启动多个线程向该ThreadLocal变量中存储一个随机值,接着各个线程调用另外其余多个类的方法,这多个类的方法中读取这个ThreadLocal变量的值,就能够看到多个类在同一个线程中共享同一份数据。code
实现对ThreadLocal变量的封装,让外界不要直接操做ThreadLocal变量。
对基本类型的数据的封装,这种应用相对不多见。
对对象类型的数据的封装,比较常见,即让某个类针对不一样线程分别建立一个独立的实例对象。对象
package com.ljq.test.thread; import java.util.Random; /** * ThreadLocal类及应用技巧 * * 将线程范围内共享数据进行封装,封装到一个单独的数据类中,提供设置获取方法 * 将该类单例化,提供获取实例对象的方法,获取到的实例对象是已经封装好的当前线程范围内的对象 */ 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(); myData.setName("name" + data); myData.setAge(data); myThreadScopeData.set(myData); */ MyThreadScopeData.getThreadInstance().setName("name" + data); MyThreadScopeData.getThreadInstance().setAge(data); new A().get(); new B().get(); } }).start(); } } //使用获取到的线程范围内的对象实例调用相应方法 static class A{ public void get(){ int data = x.get(); System.out.println("A from " + Thread.currentThread().getName() + " get data :" + data); /* MyThreadScopeData myData = myThreadScopeData.get(); System.out.println("A from " + Thread.currentThread().getName() + " getMyData: " + myData.getName() + "," + myData.getAge()); */ MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); System.out.println("A from " + Thread.currentThread().getName() + " getMyData: " + myData.getName() + "," + myData.getAge()); } } //使用获取到的线程范围内的对象实例调用相应方法 static class B{ public void get(){ int data = x.get(); System.out.println("B from " + Thread.currentThread().getName() + " get data :" + data); MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); System.out.println("B from " + Thread.currentThread().getName() + " getMyData: " + myData.getName() + "," + myData.getAge()); } } } class MyThreadScopeData { // 单例 private MyThreadScopeData() { } // 提供获取实例方法,不加synchronized关键字表示线程各拿各自的数据,互不干扰 public static/* synchronized */MyThreadScopeData getThreadInstance() { // 从当前线程范围内数据集中获取实例对象 MyThreadScopeData instance = map.get(); if (instance == null) { instance = new MyThreadScopeData(); map.set(instance); } return instance; } // 将实例对象存入当前线程范围内数据集中 private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<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; } }
xxx
xxxxxxx
xxx
xxx
xxxblog