publicfinalbooleanrecycle(T o, Handle handle){ if (handle == NOOP_HANDLE) { returnfalse; }
DefaultHandle h = (DefaultHandle) handle; if (h.stack.parent != this) { returnfalse; } if (o != h.value) { thrownew IllegalArgumentException("o does not belong to handle"); } h.recycle(); returntrue; }
回收1个对象会调用该对象DefaultHandle.recycle()方法,如下:
publicvoidrecycle(){ stack.push(this); }
回收1个对象(DefaultHandle)就是把该对象push到stack中。
voidpush(DefaultHandle item){ Thread currentThread = Thread.currentThread(); if (thread == currentThread) { // The current Thread is the thread that belongs to the Stack, we can try to push the object now. /** * 如果该stack就是本线程的stack,那么直接把DefaultHandle放到该stack的数组里 */ pushNow(item); } else { // The current Thread is not the one that belongs to the Stack, we need to signal that the push // happens later. /** * 如果该stack不是本线程的stack,那么把该DefaultHandle放到该stack的WeakOrderQueue中 */ pushLater(item, currentThread); } }
int size = this.size; if (size >= maxCapacity || dropHandle(item)) { // Hit the maximum capacity or should drop - drop the possibly youngest object. return; } if (size == elements.length) { elements = Arrays.copyOf(elements, min(size << 1, maxCapacity)); }
privatevoidpushLater(DefaultHandle item, Thread thread){ /** * Recycler有1个stack->WeakOrderQueue映射,每个stack会映射到1个WeakOrderQueue,这个WeakOrderQueue是该stack关联的其它线程WeakOrderQueue链表的head WeakOrderQueue。 * 当其它线程回收对象到该stack时会创建1个WeakOrderQueue中并加到stack的WeakOrderQueue链表中。 */ Map<Stack<?>, WeakOrderQueue> delayedRecycled = DELAYED_RECYCLED.get(); WeakOrderQueue queue = delayedRecycled.get(this); if (queue == null) { /** * 如果delayedRecycled满了那么将1个伪造的WeakOrderQueue(DUMMY)放到delayedRecycled中,并丢弃该对象(DefaultHandle) */ if (delayedRecycled.size() >= maxDelayedQueues) { // Add a dummy queue so we know we should drop the object delayedRecycled.put(this, WeakOrderQueue.DUMMY); return; } // Check if we already reached the maximum number of delayed queues and if we can allocate at all. /** * 创建1个WeakOrderQueue */ if ((queue = WeakOrderQueue.allocate(this, thread)) == null) { // drop object return; } delayedRecycled.put(this, queue); } elseif (queue == WeakOrderQueue.DUMMY) { // drop object return; }
static WeakOrderQueue allocate(Stack<?> stack, Thread thread){ // We allocated a Link so reserve the space /** * 如果该stack的可用共享空间还能再容下1个WeakOrderQueue,那么创建1个WeakOrderQueue,否则返回null */ return reserveSpace(stack.availableSharedCapacity, LINK_CAPACITY) ? new WeakOrderQueue(stack, thread) : null; }
if (srcStart != srcEnd) { /** * head Link的DefaultHandle数组 */ final DefaultHandle[] srcElems = head.elements; /** * stack的DefaultHandle数组 */ final DefaultHandle[] dstElems = dst.elements; int newDstSize = dstSize; /** * 迁移head Link的DefaultHandle数组到stack的DefaultHandle数组 */ for (int i = srcStart; i < srcEnd; i++) { DefaultHandle element = srcElems[i]; if (element.recycleId == 0) { element.recycleId = element.lastRecycledId; } elseif (element.recycleId != element.lastRecycledId) { thrownew IllegalStateException("recycled already"); } srcElems[i] = null;
if (dst.dropHandle(element)) { // Drop the object. continue; } element.stack = dst; dstElems[newDstSize ++] = element; }
/** * 当head节点的对象全都转移给stack后,取head下一个节点作为head,下次转移的时候再从新的head转移回收的对象 */ if (srcEnd == LINK_CAPACITY && head.next != null) { // Add capacity back as the Link is GCed. reclaimSpace(LINK_CAPACITY);
this.head = head.next; } /** * 迁移完成后更新原始head Link的readIndex */ head.readIndex = srcEnd; if (dst.size == newDstSize) { returnfalse; } dst.size = newDstSize; returntrue; } else { // The destination stack is full already. returnfalse; } }