package com.verser.weixin.test; public class Test { static InheritableThreadLocal<Object> ITL = new InheritableThreadLocal<Object>(){ protected Object initialValue() { return new StringBuffer("hello "); }; }; static ThreadLocal<Object> TL = new ThreadLocal<Object>(){ protected Object initialValue() { return new StringBuffer("HELLO "); }; }; public static void main(String[] args) throws Exception { System.out.println("Thread-t " + " , " + ITL.get() + " , " + TL.get()); Thread t_1 = new Thread(new Runnable() { @Override public void run() { System.out.println("Thread-t_1 " + " , " + ITL.get() + " , " + TL.get()); ((StringBuffer)ITL.get()).append("wonderful "); ((StringBuffer)TL.get()).append("WONDERFUL "); Thread t_1_1 = new Thread(new Runnable() { @Override public void run() { System.out.println("Thread-t_1_1 " + " , " + ITL.get() + " , " + TL.get()); ((StringBuffer)ITL.get()).append("world"); ((StringBuffer)TL.get()).append("WORLD"); System.out.println("Thread-t_1_1 " + " , " + ITL.get() + " , " + TL.get()); } }); t_1_1.start(); try { t_1_1.join(); System.out.println("Thread-t_1 " + " , " + ITL.get() + " , " + TL.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t_1.start(); t_1.join(); System.out.println("Thread-t " + " , " + ITL.get() + " , " + TL.get()); } }
Thread-t , hello , HELLO Thread-t_1 , hello , HELLO Thread-t_1_1 , hello wonderful , HELLO Thread-t_1_1 , hello wonderful world , HELLO WORLD Thread-t_1 , hello wonderful world , HELLO WONDERFUL Thread-t , hello wonderful world , HELLO
相关推荐
NULL 博文链接:https://jnh.iteye.com/blog/2000288
ThreadLocal、InheritableThreadLocal详解 多线程访问同一个共享变量时,容易出现并发冲突,为了保证线程的安全,一般使用者在访问共享变量时,需要进行适量的同步。而ThreadLocal提供了线程的私有变量,每个线程都...
Synchronized与ThreadLocal
Android 详解ThreadLocal及InheritableThreadLocal 概要: 因为在android中经常用到handler来处理异步任务,通常用于接收消息,来操作UIThread,其中提到涉及到的looper对象就是保存在Threadlocal中的,因此研究下...
ThreadLocal应用示例及理解,这个写了相关的示例,可以参考一下。
ThreadLocal入门教程。 讲解了线程安全和ThreadLocal的使用的基本知识。
ThreadLocal
理解ThreadLocal 理解ThreadLocal 理解ThreadLocal 理解ThreadLocal
学习ThreadLocal,了解其中的原理,以及学习其中的优点!避免坑点!!
正确理解ThreadLocal.pdf
Java中ThreadLocal的设计与使用.doc
java 简单的ThreadLocal示例
ThreadLocal的几种误区ThreadLocal的几种误区ThreadLocal的几种误区
主要介绍ThreadLocal的原理,实例分析以及注意事项
DbUTils中用ThreadLocal类
详解java底层实现原理,ThreadLocal底层实现的数据结构,为什么不会导致内存泄露
JDBC事务的封装和Threadlocal实例,参考博客:http://blog.csdn.net/daijin888888/article/details/50988053