ThreadLocal的使用方法

 一、ThreadLocal是指线程的局部变量,查看源码可以知道一个Map的方式。

package com.yezi.learn.local;

import java.util.Random;

/**
 * Created by yezi on 2014/4/13.
 */
public class TestThreadLocal {
    private static ThreadLocal<TLUser> threadLocal = new ThreadLocal<TLUser>();
    public static void main(String []args){
        for(int i=0;i<2;i++){
            new Thread(
                    new Runnable() {
                        @Override
                        public void run() {
                            TLUser tlUser = new TLUser();
                            tlUser.setX(new Random().nextInt());
                            threadLocal.set(tlUser);
                            System.out.println("put"+tlUser);
                            new A().getData();
                            new B().getData();
                        }
                    }
            ).start();
        }
    }

    static class A{
        public void getData(){
            System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
        }
    }

    static class B{
        public void getData(){
            System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
        }
    }
}

class TLUser{
    private int x;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    @Override
    public String toString() {
        return ""+x;
    }
}

二、ThreadLocal的第二种用法

package com.yezi.learn.local;

import java.util.Random;

/**
 * Created by yezi on 2014/4/13.
 */
public class TestThreadLocal1 {

    public static void main(String args[]){
        for(int i=0;i<2;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ThreadScopeData.getThreadInstance().setName("xiaoyezi"+new Random().nextInt());
                    new A().outData();
                    new B().outData();
                }
            }).start();
        }
    }

    static class A{
        public void outData(){
            System.out.println("A:"+Thread.currentThread().getName()+ThreadScopeData.getThreadInstance());
        }
    }

    static class B{
        public void outData(){
            System.out.println("B:"+Thread.currentThread().getName()+ThreadScopeData.getThreadInstance());
        }
    }
}

class ThreadScopeData{

    private ThreadScopeData(){}

    private String name;

    private static ThreadLocal<ThreadScopeData> threadLocal = new ThreadLocal<ThreadScopeData>();

    public static ThreadScopeData getThreadInstance(){
        ThreadScopeData threadScopeData = threadLocal.get();
        if(threadScopeData==null){
            threadScopeData = new ThreadScopeData();
            threadLocal.set(threadScopeData);
        }
        return threadScopeData;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

三、总结

1、ThreadLocal是使用的一种线程的局部变量。

2、使用ThreadLocal可以很好每次都使用开始创建的那个对象。

3、Hibernate中就使用ThreadLocal来获取当前事务的处理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值