(5)ThreadLocal类及应用技巧

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 实例是可访问的;

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





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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于多线程编程,ThreadLocal是一个非常有用的工具。它提供了线程本地变量的机制,使得每个线程都可以独立地存储和获取自己的变量副本。这在一些场景下非常有用,下面是一些ThreadLocal应用技巧: 1. 线程上下文信息传递:在多线程环境中,有时需要在线程之间传递一些上下文信息,例如用户身份认证信息、语言环境等。使用ThreadLocal可以很方便地实现这一目的,每个线程都有自己独立的上下文信息副本,互不干扰。 2. 线程安全的数据存储:在多线程环境中,如果多个线程需要共享一份数据,可以使用ThreadLocal来维护每个线程的局部副本。这样每个线程都可以独立地修改自己的副本,而不会影响其他线程的数据。 3. 避免传参:有些情况下,我们需要在多个方法之间传递某些参数,但是参数传递会增加代码复杂性。使用ThreadLocal可以避免传参,每个方法可以直接从ThreadLocal中获取需要的参数值。 4. 事务管理:在一些事务场景中,我们可能需要在同一个线程中执行多个数据库操作,并保证这些操作在同一个数据库事务中。使用ThreadLocal可以在线程中维护一个数据库连接对象,确保多个操作都使用同一个连接,实现事务的一致性。 需要注意的是,ThreadLocal虽然提供了线程本地变量的机制,但并不是万能的解决方案。在使用ThreadLocal时,需要注意内存泄漏的问题,及时清理不再使用的资源。 希望以上内容对您有帮助!如需了解更多多线程编程相关内容,可以参考CSDN中张孝祥的相关文章。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值