来自: http://blog.csdn.net//chenleixing/article/details/42373507 ThreadLocal总结 1、ThreadLocal使用场合主要解决多线程中数据数据因并发产生不一致问题。ThreadLocal为每个线程的中并发访问的数据提供一个副本,通过访问副本来运行业务,这样的结果是耗费了内存,单大大减少了线程同步所带来性能消耗,也减少了线程并发控制的复杂度。 2、ThreadLocal不能使用原子类型,只能使用Object类型。ThreadLocal的使用比synchronized要简单得多。 3、ThreadLocal和Synchonized都用于解决多线程并发访问。但是ThreadLocal与synchronized有本质的区别。synchronized是利用锁的机制,使变量或代码块在某一时该只能被一个线程访问。而ThreadLocal为每一个线程都提供了变量的副本,使得每个线程在某时间访问到的并不是同一个对象,这样就隔离了多个线程对数据的数据共享。而Synchronized却正好相反,它用于在多个线程间通信时能够获得数据共享。 4、Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离。 5、ThreadLocal并不能替代synchronized,它们处理不同的问题域。Synchronized用于实现同步机制,比ThreadLocal更加复杂。 - import java.util.Random;
- /**
- * ThreadLocal实例
- * 源自http://lavasoft.blog.51cto.com/62575/51926/
- * @author yinchuan.chen
- *
- */
- public class ThreadLocalDemo implements Runnable {
- //创建线程局部变量studentLocal,在后面你会发现用来保存Student对象
- private final static ThreadLocal studentLocal = new ThreadLocal();
- public static void main(String[] agrs) {
- ThreadLocalDemo td = new ThreadLocalDemo();
- Thread t1 = new Thread(td, "a");
- Thread t2 = new Thread(td, "b");
- t1.start();
- t2.start();
- }
- public void run() {
- accessStudent();
- }
- /**
- * 示例业务方法,用来测试
- */
- public void accessStudent() {
- //获取当前线程的名字
- String currentThreadName = Thread.currentThread().getName();
- System.out.println(currentThreadName + " is running!");
- //产生一个随机数并打印
- Random random = new Random();
- int age = random.nextInt(100);
- System.out.println("thread " + currentThreadName + " set age to:" + age);
- //获取一个Student对象,并将随机数年龄插入到对象属性中
- Student student = getStudent();
- student.setAge(age);
- System.out.println("thread " + currentThreadName + " first read age is:" + student.getAge());
- try {
- Thread.sleep(500);
- }
- catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- System.out.println("thread " + currentThreadName + " second read age is:" + student.getAge());
- }
- protected Student getStudent() {
- //获取本地线程变量并强制转换为Student类型
- Student student = (Student) studentLocal.get();
- //线程首次执行此方法的时候,studentLocal.get()肯定为null
- if (student == null) {
- //创建一个Student对象,并保存到本地线程变量studentLocal中
- student = new Student();
- studentLocal.set(student);
- }
- return student;
- }
- }
- class Student {
- private int age = 0; //年龄
- public int getAge() {
- return this.age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
复制代码 本文源自 http://lavasoft.blog.51cto.com/62575/51926/ |