public synchronized void setSize(int newSz) { if (maxSize < newSz) { // list grew... for (int i=maxSize; i<newSz; i++) free.add(new LRUNode()); } else if (maxSize > newSz) { for (int i=used.getSize(); i>newSz; i--) { LRUNode nde = (LRUNode)used.getTail(); used.remove(nde); nde.setObj(null); } } maxSize = newSz; }
public LRUCache(int size) { if (size <= 0) size=1; maxSize = size; free = new DoublyLinkedList(); used = new DoublyLinkedList(); while (size > 0) { free.add(new LRUNode()); size--; } }
public synchronized void add(LRUObj obj) { LRUNode nde = obj.lruGet(); // already linked in... if (nde != null) { used.touch(nde); return; } if (free.getSize() > 0) { nde = (LRUNode)free.pop(); nde.setObj(obj); used.add(nde); } else { nde = (LRUNode)used.getTail(); nde.setObj(obj); used.touch(nde); } }
/** * Removes all elements from the list. */ public synchronized void empty() { while(size > 0) pop(); }
/** * Schedules the given Runnable object for a later invocation, and * returns. The given runnable preempts any runnable that is not * currently executing (ie the next runnable started will be the * one given). An exception is thrown if the RunnableQueue was * not started. * @throws IllegalStateException if getThread() is null. */ public void preemptLater(Runnable r) { if (runnableQueueThread == null) { throw new IllegalStateException ("RunnableQueue not started or has exited"); } synchronized (list) { list.add(preemptCount, new Link(r)); preemptCount++; list.notify(); } }
/** * Schedules the given Runnable object for a later invocation, and * returns. * An exception is thrown if the RunnableQueue was not started. * @throws IllegalStateException if getThread() is null. */ public void invokeLater(Runnable r) { if (runnableQueueThread == null) { throw new IllegalStateException ("RunnableQueue not started or has exited"); } synchronized (list) { list.push(new Link(r)); list.notify(); } }
/** * Removes 'head' from list and returns it. Returns null if list is empty. * @return current head element, next element becomes head. */ public Node pop() { if (head == null) return null; Node nde = head; remove(nde); return nde; }
Link head = (Link)list.getHead(); Link link; public boolean hasNext() {
public synchronized void touch(LRUObj obj) { LRUNode nde = obj.lruGet(); if (nde == null) return; used.touch(nde); }
public int getUsed() { return used.getSize(); }
public synchronized void add(LRUObj obj) { LRUNode nde = obj.lruGet(); // already linked in... if (nde != null) { used.touch(nde); return; } if (free.getSize() > 0) { nde = (LRUNode)free.pop(); nde.setObj(obj); used.add(nde); } else { nde = (LRUNode)used.getTail(); nde.setObj(obj); used.touch(nde); } }
public LRUCache(int size) { if (size <= 0) size=1; maxSize = size; free = new DoublyLinkedList(); used = new DoublyLinkedList(); while (size > 0) { free.add(new LRUNode()); size--; } }
/** * Removes all elements from the list. */ public synchronized void empty() { while(size > 0) pop(); }
/** * Schedules the given Runnable object for a later invocation, and * returns. The given runnable preempts any runnable that is not * currently executing (ie the next runnable started will be the * one given). An exception is thrown if the RunnableQueue was * not started. * @throws IllegalStateException if getThread() is null. */ public void preemptLater(Runnable r) { if (runnableQueueThread == null) { throw new IllegalStateException ("RunnableQueue not started or has exited"); } synchronized (list) { list.add(preemptCount, new Link(r)); preemptCount++; list.notify(); } }