public class TestThreadLocal {
ThreadLocal<String> myLocal = new ThreadLocal<String>();
String myName;
public String getName() {
return myName;
}
public void setName(String name) {
myName = name;
}
public static void main(String[] args) throws InterruptedException {
TestThreadLocal ooo = new TestThreadLocal();
Thread[] threadArray = new Thread[10];
for (int i = 0; i < 10; i++) {
Thread thi = new Thread(new ThreadWithoutThreadLocal(ooo), "thread" + i);
threadArray[i] = thi;
}
for (Thread th : threadArray) {
th.start();
}
Thread.sleep(3000);
System.out.println("-----------------------------------------------------------------");
for (int i = 0; i < 10; i++) {
Thread thi = new Thread(new ThreadWithThreadLocal(ooo), "thread" + i);
threadArray[i] = thi;
}
for (Thread th : threadArray) {
th.start();
}
}
}
class ThreadWithoutThreadLocal implements Runnable {
public TestThreadLocal ooo;
public ThreadWithoutThreadLocal(TestThreadLocal o) {
ooo = o;
}
@Override
public void run() {
String localName = Thread.currentThread().getName();
ooo.setName(localName);
//ooo.myLocal.set(localName);
// it will be mess up if multi thread running
// result like this
// ThreadName =======thread1 but it's actual name thread1
// ThreadName =======thread2 but it's actual name thread2
// ThreadName =======thread1 but it's actual name thread0
// ThreadName =======thread4 but it's actual name thread4
// ThreadName =======thread3 but it's actual name thread3
// ThreadName =======thread7 but it's actual name thread7
// ThreadName =======thread6 but it's actual name thread6
// ThreadName =======thread5 but it's actual name thread5
// ThreadName =======thread8 but it's actual name thread8
// ThreadName =======thread9 but it's actual name thread9
System.out.println(
"ThreadName =======" + ooo.getName() + " but it's actual name " + Thread.currentThread().getName());
}
}
class ThreadWithThreadLocal implements Runnable {
public TestThreadLocal ooo;
public ThreadWithThreadLocal(TestThreadLocal o) {
ooo = o;
}
@Override
public void run() {
String localName = Thread.currentThread().getName();
ooo.myLocal.set(Thread.currentThread().getName());
// ThreadName======thread0 but it's actual name thread0
// ThreadName======thread2 but it's actual name thread2
// ThreadName======thread1 but it's actual name thread1
// ThreadName======thread3 but it's actual name thread3
// ThreadName======thread5 but it's actual name thread5
// ThreadName======thread4 but it's actual name thread4
// ThreadName======thread8 but it's actual name thread8
// ThreadName======thread6 but it's actual name thread6
// ThreadName======thread9 but it's actual name thread9
// ThreadName======thread7 but it's actual name thread7
System.out.println("ThreadName======" + ooo.myLocal.get() + " but it's actual name " + localName);
}
}
相关推荐
ThreadLocal应用示例及理解,这个写了相关的示例,可以参考一下。
javaee开发常见的模式有MVC模式,在C层中常常会再次分层,如:servlet(web层)、service(业务逻辑层)、dao(数据访问层),其中service和dao最容易混在一起,如转...所以,使用ThreadLocal可以解决这样的分层问题。
ThreadLocal
学习ThreadLocal,了解其中的原理,以及学习其中的优点!避免坑点!!
正确理解ThreadLocal.pdf
ThreadLocal的几种误区ThreadLocal的几种误区ThreadLocal的几种误区
主要介绍ThreadLocal的原理,实例分析以及注意事项
ThreadLocal保证一个类的实例变量在各个线程中都有一份单独的拷贝, 从而不会影响其他线程中的实例变量
JDBC事务的封装和Threadlocal实例,参考博客:http://blog.csdn.net/daijin888888/article/details/50988053
java 简单的ThreadLocal示例
本例以序列号生成的程序为例,展示ThreadLocal的使用
理解ThreadLocal 理解ThreadLocal 理解ThreadLocal 理解ThreadLocal
详解java底层实现原理,ThreadLocal底层实现的数据结构,为什么不会导致内存泄露
设计模式及ThreadLocal详细讲解资料,想要学习java或者提升自己技术的同学可以下载观看
ThreadLocal的基本原理,核心机制,源码,ThreadLocal在分布式架构中的应用,ThreadLocal在基础架构,开源中间件,使用非常广泛,建议掌握。
ThreadLocal源码(版本:Android4.3,,含注释)
ThreadLocal原理及在多层架构中的应用