ThreadLocal的用法

 

ThreadLocal的核心思想很简单:为每个独立的线程提供一个变量的副本。

Java提供的synchronized关键字使用了“同步锁”的机制来阻止线程的竞争访问,即“以时间换空间”。: " 10pt; FONT-SIZE:>    ThreadLocal则使用了“拷贝副本”的方式,人人有份,你用你的,我用我的,大家互不影响,是“以空间换时间”。每个线程修改变量时,实际上修改的是变量的副本,不怕影响到其它线程。

为了加深对ThreadLocal的理解,下面我使用一个例子来演示ThreadLocal如何隔离线程间的变量访问和修改:

package threadLocal;

import java.util.Random;

public class ThreadLocalTest implements Runnable{
    
    ThreadLocal<Studen> studenThreadLocal = new ThreadLocal<Studen>();

    public void run() {
        String currentThreadName = Thread.currentThread().getName();
        System.out.println(currentThreadName + " is running...");
        Random random = new Random();
        int age = random.nextInt(100);
        System.out.println(currentThreadName + " is set age: "  + age);
        Studen studen = getStudent(); //通过这个方法,为每个线程都独立的new一个student对象,每个线程的的student对象都可以设置不同的值
        studen.setAge(age);
        System.out.println(currentThreadName + " is first get age: " + studen.getAge());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println( currentThreadName + " is second get age: " + studen.getAge());
        
    }
    
    private Studen getStudent() {
        Studen studen = studenThreadLocal.get();
        if (null == studen) {
            studen = new Studen();
            studenThreadLocal.set(studen);
        }
        return studen;
    }

    public static void main(String[] args) {
        ThreadLocalTest t = new ThreadLocalTest();
        Thread t1 = new Thread(t,"Thread A");
        Thread t2 = new Thread(t,"Thread B");
        t1.start();
        t2.start();
    }
    
}

class Studen{
    int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}

 package threadLocal;

import java.util.Random;

public class MultiThreadTest  implements Runnable{
    
    Studen studen = new Studen();
    
    public void run() {
        String currentThreadName = Thread.currentThread().getName();
        System.out.println(currentThreadName + " is running ....");
        //同步
        synchronized (studen) {
            Random random = new Random();
            int age = random.nextInt(100);
            studen.setAge(age);
            System.out.println(currentThreadName + " is set age: " + age);
            System.out.println(currentThreadName + "is first get age: " + studen.getAge() );
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(currentThreadName + " is second get age: " + studen.getAge() );
        }
    }
    
    public static void main(String[] args) {
        MultiThreadTest m = new MultiThreadTest();
        Thread t1 = new Thread(m,"Thread A");
        Thread t2 = new Thread(m,"Thread B");
        t1.start();
        t2.start();
    }
}

class Student {
    int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}

  以上2中方法都实现的功能相同,但方法不一样 

ThreadLocal使用场合主要解决多线程中数据数据因并发产生不一致问题。ThreadLocal为每个线程的中并发访问的数据提供一个副本,通过访问副本来运行业务,这样的结果是耗费了内存,单大大减少了线程同步所带来性能消耗,也减少了线程并发控制的复杂度。 

ThreadLocal不能使用原子类型,只能使用Object类型。ThreadLocal的使用比synchronized要简单得多。 

ThreadLocal和Synchonized都用于解决多线程并发访问。但是ThreadLocal与synchronized有本质的区 别。synchronized是利用锁的机制,使变量或代码块在某一时该只能被一个线程访问。而ThreadLocal为每一个线程都提供了变量的副本, 使得每个线程在某一时间访问到的并不是同一个对象,这样就隔离了多个线程对数据的数据共享。而Synchronized却正好相反,它用于在多个线程间通 信时能够获得数据共享。 

Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离。 

当然ThreadLocal并不能替代synchronized,它们处理不同的问题域。Synchronized用于实现同步机制,比ThreadLocal更加复杂。 

ThreadLocal使用的一般步骤 

1、在多线程的类(如ThreadDemo类)中,创建一个ThreadLocal对象threadXxx,用来保存线程间需要隔离处理的对象xxx。 
2、在ThreadDemo类中,创建一个获取要隔离访问的数据的方法getXxx(),在方法中判断,若ThreadLocal对象为null时候,应该new()一个隔离访问类型的对象,并强制转换为要应用的类型。 
3、在ThreadDemo类的run()方法中,通过getXxx()方法获取要操作的数据,这样可以保证每个线程对应一个数据对象,在任何时刻都操作的是这个对象。

 

案例2

 

package threadLocal2;

public class SerialNum {

    private static int nextSerialNum = 1;

    @SuppressWarnings("unchecked")
    private static ThreadLocal threadLocalNum = new ThreadLocal() {
        protected synchronized Object initialValue() {
            return new Integer(nextSerialNum++);      
        }                                                           
    };

    public static int get() {
        return ((Integer) (threadLocalNum.get())).intValue();
    }
    
    @SuppressWarnings("unchecked")
    public static void set(Integer newSerial){
        threadLocalNum.set(newSerial);
    }
}

 

 

package threadLocal2;

public class GetSerialNumThread implements Runnable {

    public static void main(String args[]) {

        GetSerialNumThread serialNumGetter = new GetSerialNumThread();
        Thread t1 = new Thread(serialNumGetter, "Thread A");
        Thread t2 = new Thread(serialNumGetter, "Thread B");
        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }    
        t2.start();            
    }

    public void run() {
        int mySerialNum = getSerialNum();
        System.out.println("线程 " + Thread.currentThread().getName()
                + " 获取到的序列号是" + mySerialNum);
        System.out.println("线程 " + Thread.currentThread().getName()
                + " 修改了序列号为" + (mySerialNum * 3));
        setSerialNum(mySerialNum * 3);
        System.out.println("线程 " + Thread.currentThread().getName()
                + " 再次获得的序列号是" + getSerialNum());
    }

    private int getSerialNum() {
        return SerialNum.get();
    }

    private void setSerialNum(int newSerialNum) {
        SerialNum.set(new Integer(newSerialNum));
    }
}

 运行的结果如下:

线程 Thread A 获取到的序列号是1

线程 Thread A 修改了序列号为3

线程 Thread A 再次获得的序列号是3

线程 Thread B 获取到的序列号是2

线程 Thread B 修改了序列号为6

线程 Thread B 再次获得的序列号是6

可见第一个线程在调用SerialNum.set(int)方法修改static变量时,其实修改的是它自己的副本,而不是修改本地变量,第二个线程在初始化的时候拿到的序列号是2而不是7。

 

为什么会这样呢?明明serialNum是静态变量啊?其实我们只需要看看ThreadLocal的内部构造就知道了:

A. ThreadLocal的get()方法:

 

/**
     * Returns the value in the current thread's copy of this thread-local
     * variable.  Creates and initializes the copy if this is the first time
     * the thread has called this method.
     *
     * @return the current thread's value of this thread-local
     */
    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            return (T)map.get(this);

        // Maps are constructed lazily.  if the map for this thread
        // doesn't exist, create it, with this ThreadLocal and its
        // initial value as its only entry.
        T value = initialValue();
        createMap(t, value);
        return value;
    }

 B. ThreadLocal的set()方法:

 

/**
     * Sets the current thread's copy of this thread-local variable
     * to the specified value.  Many applications will have no need for
     * this functionality, relying solely on the {@link #initialValue}
     * method to set the values of thread-locals.
     *
     * @param value the value to be stored in the current threads' copy of
     *        this thread-local.
     */
    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

 

可以看到ThreadLocal在内部维护了一个Map,将变量的值和线程绑定起来,get/set方法都是对该线程对应的value进行操作,所以不会影响到其它线程。

 

附 一个封装的工具类:

package org.jasig.cas.a4.tools;

import java.util.HashMap;
import java.util.Map;

public class A4ThreadLocalUtil {  
    private static ThreadLocal threadLocal = new ThreadLocal();  
      
    public static void setValue(Map map) {  
        threadLocal.set(map);  
    }  
    public static void addValue(Map map) { 
    	Map oldMap = (Map) getValue();
    	map.putAll(oldMap);
    	threadLocal.set(map);  
    }  
      
    public static Map getValue() {  
    	Map map = (Map) threadLocal.get();  
        if(map == null) {  
            map = new HashMap();  
        }  
        return map;  
    }  
      
    public static  void remove() {  
        threadLocal.remove();  
    }  
}  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值