Tapestry3源码阅读笔记3:Pool/PoolList类/JanitorThread
前面说到实际保存page的是Pool类,那么来看Pool类。
public synchronized Object retrieve(Object key){Object result = null;if (_map == null)_map = new HashMap();PoolList list = (PoolList) _map.get(key);if (list != null)result = list.retrieve();if (result != null)_pooledCount--;return result;}?Pool类的store方法,保存缓存的对象public synchronized void store(Object key, Object object){getAdaptor(object).resetForPool(object);if (_map == null)_map = new HashMap();PoolList list = (PoolList) _map.get(key);if (list == null){list = new PoolList(this);_map.put(key, list);}int count = list.store(_generation, object);_pooledCount++;}?Pool类的executeClean方法,负责执行清除过老的缓存:public synchronized void executeCleanup(){_generation++;int oldestGeneration = _generation - _window;if (oldestGeneration < 0)return;int oldCount = _pooledCount;int culledKeys = 0;// During the cleanup, we keep the entire instance synchronized// (meaning other threads will block when trying to store// or retrieved pooled objects). Fortunately, this// should be pretty darn quick!int newCount = 0;Iterator i = _map.entrySet().iterator();while (i.hasNext()){Map.Entry e = (Map.Entry) i.next();PoolList list = (PoolList) e.getValue();int count = list.cleanup(oldestGeneration);if (count == 0){i.remove();culledKeys++;}elsenewCount += count;}_pooledCount = newCount;}?PoolList类的cleanup方法:public int cleanup(int generation){_spare = null;_count = 0;Entry prev = null;// Walk through the list. They'll be sorted by generation.Entry e = _first;while (true){if (e == null)break;// If found a too-old entry then we want to// delete it.if (e.generation <= generation){Object pooled = e.pooled;// Notify the object that it is being dropped// through the cracks!_pool.getAdaptor(pooled).discardFromPool(pooled);// Set the next pointer of the previous node to null.// If the very first node inspected was too old,// set the first pointer to null.if (prev == null)_first = null;elseprev.next = null;}else_count++;prev = e;e = e.next;}return _count;}?JanitroThread类的主要方法:protected void sweep(){synchronized (references){Iterator i = references.iterator();while (i.hasNext()){WeakReference ref = (WeakReference) i.next();ICleanable cleanable = (ICleanable) ref.get();if (cleanable == null)i.remove();elsecleanable.executeCleanup();}}}??