/**
* ThreadLocalShareData.java
* cn.com.songjy.test.socket.thread
* Function: TODO
*
* version date author
* ──────────────────────────────────
* 1.0 2013-8-16 songjy
*
* Copyright (c) 2013, TNT All Rights Reserved.
*/
package cn.com.songjy.test.socket.thread;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* ClassName:ThreadLocalShareData
* ThreadLocal类及应用技巧
* @author songjy
* @version 1.0
* @since v1.0
* @Date 2013-8-16 下午4:14:56
*/
public class ThreadLocalShareData {
private static Log log = LogFactory.getLog(ThreadLocalShareData.class);
private static ThreadLocal<Integer> thread_data = new ThreadLocal<Integer>();
//多个变量要求在线程范围内共享可以封装到一个对象中进行存储
private static ThreadLocal<MyThreadScopeData> my_thread_data = new ThreadLocal<MyThreadScopeData>();
private static int data;
public static void main(String[] args) {
for(int i=0; i<5; i++)
new Thread(new Runnable() {
@Override
public void run() {
data = new Random().nextInt();
int data = new Random().nextInt();
log.info(Thread.currentThread().getName() + " has put data :" + data);
//thread_data.put(Thread.currentThread(), data);
thread_data.set(data);
//my_thread_data.set(new MyThreadScopeData(""+data, 12));
MyThreadScopeData.getInstance().setName("name-"+data);
MyThreadScopeData.getInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}
static class A {
public void get(){
//int data = thread_data.get(Thread.currentThread());
int data = thread_data.get();
//MyThreadScopeData my = my_thread_data.get();
MyThreadScopeData my = MyThreadScopeData.getInstance();
log.info("A from " + Thread.currentThread().getName() + " get data :" + data);
log.info("A from " + Thread.currentThread().getName() + " get MyThreadScopeData :" + my.getName());
}
}
static class B {
public void get(){
//int data = thread_data.get(Thread.currentThread());
int data = thread_data.get();
//MyThreadScopeData my = my_thread_data.get();
MyThreadScopeData my = MyThreadScopeData.getInstance();
log.info("B from " + Thread.currentThread().getName() + " get data :" + data);
log.info("B from " + Thread.currentThread().getName() + " get MyThreadScopeData :" + my.getName());
}
}
}
//该类被编写成了线程范围内共享的数据类
class MyThreadScopeData {
private MyThreadScopeData(){}
/*单例之饱汉模式
private static MyThreadScopeData instance = new MyThreadScopeData();
public static synchronized MyThreadScopeData getInstance(){
return instance;
}*/
/*单例之饿汉模式
private static MyThreadScopeData instance = null;
public static synchronized MyThreadScopeData getInstance(){
if(null == instance)
instance = new MyThreadScopeData();
return instance;
}*/
/*线程范围内共享模式*/
public static MyThreadScopeData getInstance(){
MyThreadScopeData instance = map.get();
if(null == instance) {
instance = new MyThreadScopeData();
map.set(instance);
}
//return map.get();//也可
return instance;
}
private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();
private String name;
private int age;
/*MyThreadScopeData(String name, int age){
this.name = name;
this.age = 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;
}
}
引自
[url]http://down.51cto.com/data/443416[/url]