@Fancy-Bai
2015-03-03T11:11:30.000000Z
字数 1375
阅读 1736
Java,Java源码学习
java.lang.ThreadLocal
示例:
import java.util.Random;
/**
* <code>java.lang.ThreadLocal</code>简单示例
* <p>@Author baininghan
* <p>@Date 2015年3月3日
* <p>@version 1.0
*
*/
public class ThreadLocalDemo implements Runnable{
private static final ThreadLocal<Student> threadLocal = new ThreadLocal<Student>();
public static void main(String[] args) {
ThreadLocalDemo demo = new ThreadLocalDemo();
Thread t1 = new Thread(demo, "t1");
Thread t2 = new Thread(demo, "t2");
t1.start();
t2.start();
}
@Override
public void run() {
Student student = getStudent();
student.setAge(new Random().nextInt(100));
System.out.println("Thread[" + Thread.currentThread().getName() + "] is running...");
System.out.println("First read Student's age : " + student.getAge());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Second read Student's age : " + student.getAge());
clearStudent();
}
public Student getStudent(){
// 获取当前线程调用对象的一个局部变量(副本)
Student student = threadLocal.get();
// 第一次调用 get(),执行的是initialValue(),返回肯定是null,可以通过重写initialValue()方法,获得自己的返回值
// 第二次调用就会得到set()设置进去的值
if(student == null){
student = new Student();
threadLocal.set(student);
}
return student;
}
/** 回收Student */
public void clearStudent(){
Student student = threadLocal.get();
threadLocal.set(null);
if(student != null){
student = null;
}
}
class Student {
private int age;
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
}
参考链接:
1. 深入研究java.lang.ThreadLocal类
2. 作者个人博客