多线程环境下,或者web环境下,可以使用ThreadLocal做变量池用
import java.util.HashMap;
import java.util.Map;
public abstract class AbstractThreadContext {
public AbstractThreadContext() {
}
abstract ThreadLocal<Map<String, Object>> getThreadContext();
public void set(final String key, final Object value) {
ThreadLocal<Map<String, Object>> threadContext = this.getThreadContext();
Map<String, Object> map = (Map)threadContext.get();
if (map == null) {
map = new HashMap();
}
((Map)map).put(key, value);
threadContext.set(map);
}
public void removeKey(String key) {
ThreadLocal<Map<String, Object>> threadContext = this.getThreadContext();
Map<String, Object> map = (Map)threadContext.get();
if (map == null) {
map = new HashMap();
}
((Map)map).remove(key);
threadContext.set(map);
}
public Object get(String key) {
ThreadLocal<Map<String, Object>> threadContext = this.getThreadContext();
Map<String, Object> map = (Map)threadContext.get();
return map == null ? null : map.get(key);
}
public void clean() {
ThreadLocal<Map<String, Object>> threadContext = this.getThreadContext();
Map<String, Object> map = (Map)threadContext.get();
if (map != null) {
map.clear();
threadContext.set(map);
}
}
}
import java.util.Map;
public final class ThreadContextStoreUtil extends AbstractThreadContext {
private static ThreadLocal<Map<String, Object>> threadContext = new ThreadLocal();
private static volatile ThreadContextStoreUtil app = null;
private ThreadContextStoreUtil() {
}
public static synchronized ThreadContextStoreUtil getInstance() {
if (app == null) {
Class var0 = ThreadContextStoreUtil.class;
synchronized(ThreadContextStoreUtil.class) {
if (app == null) {
app = new ThreadContextStoreUtil();
}
}
}
return app;
}
@Override
protected ThreadLocal<Map<String, Object>> getThreadContext() {
return threadContext;
}
}
public class ThreadMain {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
ThreadContextStoreUtil.getInstance().set("1", "1");
ThreadContextStoreUtil.getInstance().set("2", "11");
ThreadContextStoreUtil.getInstance().set("3", "111");
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("1"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("2"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("3"));
}
}, "a").start();
new Thread(new Runnable() {
@Override
public void run() {
ThreadContextStoreUtil.getInstance().set("1", "2");
ThreadContextStoreUtil.getInstance().set("2", "22");
ThreadContextStoreUtil.getInstance().set("3", "222");
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("1"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("2"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("3"));
}
}, "b").start();
new Thread(new Runnable() {
@Override
public void run() {
ThreadContextStoreUtil.getInstance().set("1", "3");
ThreadContextStoreUtil.getInstance().set("2", "33");
ThreadContextStoreUtil.getInstance().set("3", "333");
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("1"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("2"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("3"));
}
}, "c").start();
new Thread(new Runnable() {
@Override
public void run() {
ThreadContextStoreUtil.getInstance().set("1", "4");
ThreadContextStoreUtil.getInstance().set("2", "44");
ThreadContextStoreUtil.getInstance().set("3", "444");
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("1"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("2"));
System.out.println(Thread.currentThread().getName() + ThreadContextStoreUtil.getInstance().get("3"));
}
}, "d").start();
}
}