posts - 262,  comments - 221,  trackbacks - 0

关于ThreadLocal的用法,之前一直不太清楚,直到最近看了网上一篇文章《深入研究java.lang.ThreadLocal类》,再结合SUN的API,才对这个类有了一定的了解。

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

我们知道在多线程的情况下,几个线程同时访问同一变量的情况很常见,Java提供的synchronized关键字使用了“同步锁”的机制来阻止线程的竞争访问,即“以时间换空间”。

ThreadLocal则使用了“拷贝副本”的方式,人人有份,你用你的,我用我的,大家互不影响,是“以空间换时间”。每个线程修改变量时,实际上修改的是变量的副本,不怕影响到其它线程。

ThreadLocal的一个最常见应用是为每个线程分配一个唯一的ID,例如线程ID,事务ID,一般保存在ThreadLocal中的变量都是很少需要修改的。

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

【1】SerialNum类

package example.thread.threadLocal;

public class SerialNum {

    
private static int nextSerialNum = 1;

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

        }                                                           
    };

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

【2】GetSerialNumThread
package example.thread.threadLocal;

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进行操作,所以不会影响到其它线程。

-------------------------------------------------------------
生活就像打牌,不是要抓一手好牌,而是要尽力打好一手烂牌。
posted on 2008-09-05 17:10 Paul Lin 阅读(4742) 评论(1)  编辑  收藏 所属分类: J2SE


FeedBack:
# re: 【原】Java 多线程 之 ThreadLocal
2008-09-05 19:03 | 银河使者
在MyEclipse中开发基于hibernate的程序时,自动生成的Session工厂就用了ThreadLocal,以保证每一个线程只有一个Session。

实际上,ThreadLocal的基本原理就是利用线程的id作为Map的key,将一个对象作为Map的value, 以保证一个线程只有一个key-value对。由于每一个线程有一个独立的对象,而且每个线程的对象必须独立,也就是说不能有其他的线程访问当前线程的对象,这样对于每个线程来说,是顺序执行的,也就不可能发生象脏数据这样的事情了。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(21)

随笔分类

随笔档案

BlogJava热点博客

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜