ThreadLocal维护线程局部 变量或线程局部对象
2015-08-25 23:31
302 查看
import java.util.Random;
//线程范围内的共享和作用(ThreadLocal维护线程局部 变量或线程局部对象时,每一个使用他们的线程都是独立的一个副本,不会影响其他 的线程)
public class ThreadLocalTest {
//private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
public static void main(String[] args) {
for(int i=0; i<2; i++){
new Thread(new Runnable(){
@Override
public void run() {
int data = new Random().nextInt();
System.out.println(Thread.currentThread().getName() + "has put data: " + data);
//x.set(data);
MyThreadLocalData.getThreadInstance().setName("name" + data);
MyThreadLocalData.getThreadInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public void get(){
// int data = x.get();
// System.out.println("get "+Thread.currentThread().getName() + " data: " + data);
MyThreadLocalData myData = MyThreadLocalData.getThreadInstance();
System.out.println("A from "+Thread.currentThread().getName() + " getMydata: " + myData.getName() + "," +
myData.getAge());
}
}
static class B{
public void get(){
// int data = x.get();
// System.out.println("get "+Thread.currentThread().getName() + " data: " + data);
MyThreadLocalData myData = MyThreadLocalData.getThreadInstance();
System.out.println("B from "+Thread.currentThread().getName() + " getMydata: " + myData.getName() + "," +
myData.getAge());
}
}
static class MyThreadLocalData{
private MyThreadLocalData(){}
public static /*synchronized*/ MyThreadLocalData getThreadInstance(){
MyThreadLocalData instance = map.get();
if(instance == null){
instance = new MyThreadLocalData();
map.set(instance);
}
return instance;
}
//极寒模式
//private static ThreadLocal<MyThreadLocalData> x = null;
//饱含模式
private static ThreadLocal<MyThreadLocalData> map = new ThreadLocal<MyThreadLocalData>();
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
//线程范围内的共享和作用(ThreadLocal维护线程局部 变量或线程局部对象时,每一个使用他们的线程都是独立的一个副本,不会影响其他 的线程)
public class ThreadLocalTest {
//private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
public static void main(String[] args) {
for(int i=0; i<2; i++){
new Thread(new Runnable(){
@Override
public void run() {
int data = new Random().nextInt();
System.out.println(Thread.currentThread().getName() + "has put data: " + data);
//x.set(data);
MyThreadLocalData.getThreadInstance().setName("name" + data);
MyThreadLocalData.getThreadInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public void get(){
// int data = x.get();
// System.out.println("get "+Thread.currentThread().getName() + " data: " + data);
MyThreadLocalData myData = MyThreadLocalData.getThreadInstance();
System.out.println("A from "+Thread.currentThread().getName() + " getMydata: " + myData.getName() + "," +
myData.getAge());
}
}
static class B{
public void get(){
// int data = x.get();
// System.out.println("get "+Thread.currentThread().getName() + " data: " + data);
MyThreadLocalData myData = MyThreadLocalData.getThreadInstance();
System.out.println("B from "+Thread.currentThread().getName() + " getMydata: " + myData.getName() + "," +
myData.getAge());
}
}
static class MyThreadLocalData{
private MyThreadLocalData(){}
public static /*synchronized*/ MyThreadLocalData getThreadInstance(){
MyThreadLocalData instance = map.get();
if(instance == null){
instance = new MyThreadLocalData();
map.set(instance);
}
return instance;
}
//极寒模式
//private static ThreadLocal<MyThreadLocalData> x = null;
//饱含模式
private static ThreadLocal<MyThreadLocalData> map = new ThreadLocal<MyThreadLocalData>();
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
相关文章推荐
- Linux 系统内核空间与用户空间通信的实现与分析
- linux操作
- Linux系统内部的名称解析与安全认证
- Linux下Sed的用法详情
- 在Linux、ubuntu 11下安装无线网卡 、杂牌无线网卡、ndiswrapper的使用
- Linux下VIM文本内容替换
- python 设置linux环境变量
- Linux文件系统管理
- nginx+keepalived双主高可用负载均衡
- linux 中mdelay() 与msleep()的区别
- dropdown模式下可以设置Prompt的Spinner
- 页面架构
- linux进程编程入门
- 安装Apache时可能会遇到的问题
- centos6.5 64位安装MySQL5.6
- apache常见问题处理
- linux磁盘管理20105082502
- keepalived+nginx实现双机热备
- Linux用户态与内核态交互数据---socket函数
- Linux源码包的安装