存放 DefaultHandle private static final class StackT { // stac
netty 是用 Recycler 实现东西池。
每个线程有一个 ThreadLocalMap 变量,ThreadLocalMap 素质是一个哈希表,用 index + 1 来制止槽斗嘴,键是 ThreadLocal 变量,值是尖括号里的东西。netty 里面大量使用 ThreadLocal,目的是减少多线程之间锁竞争。
// 每个线程一个 Stack,用作东西池 private final FastThreadLocal<Stack<T>> threadLocal = new FastThreadLocal<Stack<T>>() { @Override protected Stack<T> initialValue() { return new Stack<T>(Recycler.this, Thread.currentThread(), maxCapacityPerThread, maxSharedCapacityFactor, interval, maxDelayedQueuesPerThread); } @Override protected void onRemoval(Stack<T> value) { // Let us remove the WeakOrderQueue from the WeakHashMap directly if its safe to remove some overhead if (value.threadRef.get() == Thread.currentThread()) { if (DELAYED_RECYCLED.isSet()) { DELAYED_RECYCLED.get().remove(value); } } } };
// 每个线程有一个 Map,存储其他线程的 Stack -> WeakOrderQueue 对 private static final FastThreadLocal<Map<Stack<?>, WeakOrderQueue>> DELAYED_RECYCLED = new FastThreadLocal<Map<Stack<?>, WeakOrderQueue>>() { @Override protected Map<Stack<?>, WeakOrderQueue> initialValue() { return new WeakHashMap<Stack<?>, WeakOrderQueue>(); } };
DefaultHandle 封装了东西,,是直接存放在 Stack 中的元素
private static final class DefaultHandle<T> implements Handle<T> { Stack<?> stack; Object value; DefaultHandle(Stack<?> stack) { this.stack = stack; } }
Stack 是最直接的东西池,用数组实现的栈,存放 DefaultHandle
private static final class Stack<T> { // stack 属于某一个线程,用弱引用 final WeakReference<Thread> threadRef; DefaultHandle<?>[] elements; }
如果我们获取一个东西,需要挪用
public final T get() { // 没有启用东西池 if (maxCapacityPerThread == 0) { return newObject((Handle<T>) NOOP_HANDLE); } // 获取当前东西的 Stack Stack<T> stack = threadLocal.get(); // 从栈顶推出一个元素 DefaultHandle<T> handle = stack.pop(); if (handle == null) { // 栈中没有元素,则需要创建东西 handle = stack.newHandle(); handle.value = newObject(handle); } return (T) handle.value; }
用完东西之后,偿还
public void recycle(Object object) { if (object != value) { throw new IllegalArgumentException("object does not belong to handle"); } // DefaultHandle 绑定的 stack Stack<?> stack = this.stack; if (lastRecycledId != recycleId || stack == null) { throw new IllegalStateException("recycled already"); } stack.push(this); }
void push(DefaultHandle<?> item) { Thread currentThread = Thread.currentThread(); // 当前线程持有该 stack if (threadRef.get() == currentThread) { // 直接入栈 pushNow(item); } else { // 偿还的东西不是由当前线程获取的 pushLater(item, currentThread); } }
private void pushLater(DefaultHandle<?> item, Thread thread) { if (maxDelayedQueues == 0) { // We don‘t support recycling across threads and should just drop the item on the floor. return; } // 当前线程的 Map Map<Stack<?>, WeakOrderQueue> delayedRecycled = DELAYED_RECYCLED.get(); WeakOrderQueue queue = delayedRecycled.get(this); if (queue == null) { if (delayedRecycled.size() >= maxDelayedQueues) { // Add a dummy queue so we know we should drop the object delayedRecycled.put(this, WeakOrderQueue.DUMMY); return; } // 创建 WeakOrderQueue if ((queue = newWeakOrderQueue(thread)) == null) { // drop object return; } delayedRecycled.put(this, queue); } else if (queue == WeakOrderQueue.DUMMY) { // drop object return; } // DefaultHandle 放入 queue queue.add(item); }
那么,偿还到 WeakOrderQueue 中的元素,啥时候用到呢
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31080.html