使用ThreadLocal类 线程范围内共享变量。
package com.lyc.thinkinjava;
public class Singleton {
private static ThreadLocal<Singleton> map = new ThreadLocal<Singleton>();
private Singleton() {}
public static Singleton getInstance() {
Singleton instance = map.get();
if (instance == null) {
instance = new Singleton();
map.set(instance);
}
return instance;
}
}