
回复
平时并发编程,除了维护修改共享变量的场景,有时我们也需要为每一个线程设置一个私有的变量,进行线程隔离,java提供的ThreadLocal可以帮助我们实现,而讲到ThreadLocal则不得不讲讲java的四种引用,不同的引用类型在GC时表现是不一样的,引用类型Reference有助于我们了解如何快速回收某些对象的内存或对实例的GC控制
Integer index = new Integer(1);
String name = "csc";
String name = "csc";
//软引用的创建
SoftReference<String> softRef = new SoftReference<String>(name);
System.out.println(softRef.get());
static class User{
String name;
public User(String name){ this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
//弱引用的创建
WeakReference<User> softRef = new WeakReference<User>(new User("csc"));
System.out.println(softRef.get().getName()); //输出 csc
System.gc();
System.out.println(softRef.get()); //输出 null
//弱引用Map
WeakHashMap<String, String> map = new WeakHashMap<String, String>();
public static void main(String[] args) {
ReferenceQueue<User> queue = new ReferenceQueue<>();
PhantomReference<User> pr = new PhantomReference<User>(new User("csc"), queue);
//PhantomRefrence的get方法总是返回null,因此无法访问对应的引用对象。
System.out.println(pr.get()); // null
System.gc();
System.out.println(queue.poll()); //获取被垃圾回收的"xb"的引用ReferenceQueue
}
引用类型 | 被垃圾回收时间 | 场景 | 生存时间 |
强引用 | 从来不会 | 对象的一般状态 | JVM停止运行时终止 |
软引用 | 当内存不足时 | 对象缓存 | 内存不足时终止 |
弱引用 | 正常垃圾回收时 | 对象缓存 | 垃圾回收后终止 |
虚引用 | 正常垃圾回收时 | 跟踪对象的垃圾回收 | 垃圾回收后终止 |
ReferenceQueue<String> queue = new ReferenceQueue<String>();
WeakReference<String> pr = new WeakReference<String>("wxj", queue);
System.gc();
System.out.println(queue.poll().get()); // 获取即将被回收的字符串 wxj
public class Thread implements Runnable {
/* 当前线程对于的ThreadLocalMap实例,ThreadLocal<T>作为Key,
* T对应的对象作为value */
ThreadLocal.ThreadLocalMap threadLocals = null;
public class ThreadLocal<T> {
//ThreadLocal对象对应的hash值,使用一个静态AtomicInteger实现
private final int threadLocalHashCode = nextHashCode();
//设置value
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
//获取value
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
//获取当前线程的ThreadLocalMap,再使用对象ThreadLocal获取对应的value
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
....
//类似HashMap的类
static class ThreadLocalMap {
//使用开放地址法解决hash冲突
//如果hash出的index已经有值,通过算法在后面的若干位置寻找空位
private Entry[] table;
...
//Entry 是弱引用
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
}
final class Finalizer extends FinalReference<Object> {
private static ReferenceQueue<Object> queue = new ReferenceQueue<>();
/* Invoked by VM */
static void register(Object finalizee) {
new Finalizer(finalizee);
}
private static class FinalizerThread extends Thread {
....
public void run() {
...
for (;;) {
try {
Finalizer f = (Finalizer)queue.remove();
//这里会实现Object.finalize的调用
f.runFinalizer(jla);
....
}
static {
...
Thread finalizer = new FinalizerThread(tg);
... //执行Object.finalize的守护线程
finalizer.setDaemon(true);
finalizer.start();
}
文章转载自公众号:潜行前行