publicfinal T get(){ if (maxCapacityPerThread == 0) { // 判断Stack最大能缓存的对象个数是否为0 return newObject((Handle<T>) NOOP_HANDLE); // NOOP_HANDLE 是一个常量 } Stack<T> stack = threadLocal.get(); DefaultHandle<T> handle = stack.pop(); // 从Stack中pop出一个Handle元素 if (handle == null) { // 第一次执行到这里则为空 handle = stack.newHandle(); // 新建一个handle对象 handle.value = newObject(handle); // 给handle对象赋值 } return (T) handle.value; } // stack的pop方法 DefaultHandle<T> pop(){ int size = this.size; // stack 的对象数 if (size == 0) { if (!scavenge()) { // 异线程回收对象 returnnull; } size = this.size; if (size <= 0) { // double check, avoid races returnnull; } } size --; DefaultHandle ret = elements[size]; elements[size] = null; // As we already set the element[size] to null we also need to store the updated size before we do // any validation. Otherwise we may see a null value when later try to pop again without a new element // added before. this.size = size;
publicvoidrecycle(){ handle.recycle(this); } publicvoidrecycle(Object object){ if (object != value) { thrownew IllegalArgumentException("object does not belong to handle"); }
stack.push(this); // 将handle push到Stack中 } voidpush(DefaultHandle<?> item){ Thread currentThread = Thread.currentThread(); if (threadRef.get() == currentThread) { // 如果当前线程和创建Stack的时候保存的线程是否是同一线程 // 则执行pushNow()方法将对象放入Stack中 pushNow(item); } else { // The current Thread is not the one that belongs to the Stack // (or the Thread that belonged to the Stack was collected already), we need to signal that the push // happens later. // 不同线程回收则执行 pushLaster方法 pushLater(item, currentThread); } } privatevoidpushNow(DefaultHandle<?> item){ // 第一次回收 recycleId 和 lastRecycledId 都为0 if ((item.recycleId | item.lastRecycledId) != 0) { thrownew IllegalStateException("recycled already"); } // recycleId和lastRecycledId赋值为OWN_THREAD_ID // OWN_THREAD_ID在每一个recycle中都是唯一固定的(本质上就是一个自增的AtomicInteger) item.recycleId = item.lastRecycledId = OWN_THREAD_ID;
int size = this.size; // 判断size是否超过上限 或者 dropHandle返回为true 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)); }
elements[size] = item; this.size = size + 1; } booleandropHandle(DefaultHandle<?> handle){ if (!handle.hasBeenRecycled) { // 表示当前对象之前没有回收过 // handleRecycleCount 表示当前位置Stack回收了多少次对象(不代表回收了多少个对象) if (handleRecycleCount < interval) { handleRecycleCount++; // Drop the object. returntrue; } handleRecycleCount = 0; handle.hasBeenRecycled = true; } returnfalse; }
privatevoidpushLater(DefaultHandle<?> item, Thread thread){ if (maxDelayedQueues == 0) { // We don't support recycling across threads and should just drop the item on the floor. return; }
queue.add(item); } static WeakOrderQueue newQueue(Stack<?> stack, Thread thread){ // We allocated a Link so reserve the space if (!Head.reserveSpaceForLink(stack.availableSharedCapacity)) { returnnull; } final WeakOrderQueue queue = new WeakOrderQueue(stack, thread); // Done outside of the constructor to ensure WeakOrderQueue.this does not escape the constructor and so // may be accessed while its still constructed. stack.setHead(queue);
return queue; } // 判断线程的Stack还能不能分配LINK_CAPACITY个元素 staticbooleanreserveSpaceForLink(AtomicInteger availableSharedCapacity){ for (;;) { int available = availableSharedCapacity.get(); if (available < LINK_CAPACITY) { // LINK_CAPACITY 默认为16 returnfalse; } if (availableSharedCapacity.compareAndSet(available, available - LINK_CAPACITY)) { returntrue; } } }