ThreadLocal和InheritableThreadLocal的使用

ThreadLocal和InheritableThreadLocal的使用

 

ThreadLocal 的线程安全性稍差的堂兄弟,InheritableThreadLocal

ThreadLocal 类有一个亲戚,InheritableThreadLocal,它以相似的方式工作,但适用于种类完全不同的应用程序。创建一个线程时如果保存了所有 InheritableThreadLocal 对象的值,那么这些值也将自动传递给子线程。如果一个子线程调用 InheritableThreadLocal 的 get() ,那么它将与它的父线程看到同一个对象。为保护线程安全性,您应该只对不可变对象(一旦创建,其状态就永远不会被改变的对象)使用 InheritableThreadLocal ,因为对象被多个线程共享。 InheritableThreadLocal 很合适用于把数据从父线程传到子线程,例如用户标识(user id)或事务标识(transaction id),但不能是有状态对象,例如 JDBC Connection 。

 

 例1:InheritableThreadLocal

该类扩展了 ThreadLocal,为子线程提供从父线程那里继承的值:在创建子线程时,子线程会接收所有可继承的线程局部变量的初始值,以获得父线程所具有的值。通常,子线程的值与父线程的值是一致的;但是,通过重写这个类中的 childValue 方法,子线程的值可以作为父线程值的一个任意函数。

当必须将变量(如用户 ID 和 事务 ID)中维护的每线程属性(per-thread-attribute)自动传送给创建的所有子线程时,应尽可能地采用可继承的线程局部变量,而不是采用普通的线程局部变量。

private static final InheritableThreadLocal<Object> OBJECT= new InheritableThreadLocal<Object>();

/**
     * 保存信息在当前线程中
     *
     * @param object 要保存的对象
     */
    public static void holdObject(Object object)
    {
        OBJECT.set(object);
    }
   

/**

*当前线程及其子线程都可以取到此对象

*/
    public static Object getObject()
    {
        return Object.get();
    }

 

例2:ThreadLocal

请参照jdk6API中文参考ThreadLocal

分类: java 1242人阅读 评论(0) 收藏 举报
ThreadLocal
可以在在线程内实现一个局部变量,可以在线程的任何地方来访问,能够减少参数的传递
package  jp.co.realseed;
public   class  ThreadLocalTest  ... {
    
private static ThreadLocal tl_1 = new ThreadLocal()...{
        
//返回本线程的名字,变量1
        protected Object initialValue() ...{
            
return  "Thread name 1 : "+Thread.currentThread().getName();
        }

    }
;
    
private static ThreadLocal tl_2 = new ThreadLocal()...{
        
//返回本线程的名字,变量2
        protected Object initialValue() ...{
            
return  "Thread name 2 : "+Thread.currentThread().getName();
        }

    }
;
    
public static void main(String[] args) ...{
        
for (int i = 0; i < 10; i++...{
            
new Thread(new Runnable()...{
                
public void run() ...{
                    
new ThreadLocalTest().prt();
                }

            }
).start();
        }

    }

    
//打印本线程的名字
    public void prt()...{
        System.out.println(tl_1.get());
        System.out.println(tl_2.get());
    }

}

 

InheritableThreadLocal
可以在子线程和父线程之间共享实例,也同样是为了减少参数的传递
package  jp.co.realseed;
public   class  InheritableThreadLocalTest  ... {
    
private static InheritableThreadLocal ITL = new InheritableThreadLocal()...{
        
protected Object initialValue() ...{
            
return new StringBuffer("hello");
        }

    }
;
    
public static void main(String[] args) ...{
        System.out.println(Thread.currentThread().getName()
+" : "+ITL.get());
        
new Thread(new Runnable()...{
            
public void run() ...{
                System.out.println(Thread.currentThread().getName()
+" : "+ITL.get());
                
new Thread(new Runnable()...{
                    
public void run() ...{
                        System.out.println(Thread.currentThread().getName()
+" : "+ITL.get());
                        ((StringBuffer)ITL.get()).append(
", wqf");
                        System.out.println(Thread.currentThread().getName()
+" : "+ITL.get());
                    }

                }
).start();
                
try ...{
                    Thread.sleep(
1000);
                }
 catch (InterruptedException ex) ...{
                    ex.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName()
+" : "+ITL.get());
            }
}
).start();
        
try ...{
            Thread.sleep(
1000);
        }
 catch (InterruptedException ex) ...{
            ex.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()
+" : "+ITL.get());
    }


}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值