Advertisement
mbsimonovic

LinkedBlockingQueue GC help

Jan 11th, 2012
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.67 KB | None | 0 0
  1. abstract class BaseLinkedBlockingQueue<E> {
  2.     /**
  3.      * Linked list node class
  4.      */
  5.     static class Node<E> {
  6.         E item;
  7.         /**
  8.          * One of: - the real successor Node - this Node, meaning the successor
  9.          * is head.next - null, meaning there is no successor (this is the last
  10.          * node)
  11.          */
  12.  
  13.         Node<E> next;
  14.  
  15.         Node(E x) {
  16.             item = x;
  17.         }
  18.     }
  19.  
  20.     /** Head of linked list */
  21.     protected transient Node<E> head;
  22.  
  23.     /** Tail of linked list */
  24.     protected transient Node<E> last;
  25.  
  26.     public BaseLinkedBlockingQueue() {
  27.         last = head = new Node<E>(null);
  28.     }
  29.  
  30.     /**
  31.      * Creates a node and links it at end of queue.
  32.      *
  33.      * @param x
  34.      *            the item
  35.      */
  36.     public void enqueue(E x) {
  37.         // assert putLock.isHeldByCurrentThread();
  38.         last = last.next = new Node<E>(x);
  39.     }
  40.  
  41.     /**
  42.      * Removes a node from head of queue.
  43.      *
  44.      * @return the node
  45.      */
  46.     public abstract E dequeue();
  47.  
  48.     @Override
  49.     public String toString() {
  50.         if (head.next == null) {
  51.             return "[]";
  52.         }
  53.         StringBuilder sb = new StringBuilder();
  54.         sb.append('[');
  55.         for(Node<E> node = head.next; node != null; node = node.next) {
  56.             E e = node.item;
  57.             sb.append(e);
  58.             sb.append(", ");
  59.         }
  60.         return sb.replace(sb.length()-2, sb.length(), "]").toString();
  61.     }
  62.  
  63. }
  64.  
  65. class LinkedBlockingQueueWithoutHelp<E> extends BaseLinkedBlockingQueue<E> {
  66.     @Override
  67.     public E dequeue() {
  68.         // assert takeLock.isHeldByCurrentThread();
  69.         Node<E> first = head.next;
  70.         head.next = head; // no help for GC
  71.         head = first;
  72.         E x = first.item;
  73.         first.item = null;
  74.         return x;
  75.     }
  76. }
  77.  
  78. class OriginalLinkedBlockingQueue<E> extends BaseLinkedBlockingQueue<E> {
  79.     @Override
  80.     public E dequeue() {
  81.         // assert takeLock.isHeldByCurrentThread();
  82.         Node<E> h = head;
  83.         Node<E> first = h.next;
  84.         h.next = h; // help GC
  85.         head = first;
  86.         E x = first.item;
  87.         first.item = null;
  88.         return x;
  89.     }
  90. }
  91.  
  92. public class Demo {
  93.     static class Item {
  94.         Double x;
  95.         Double y;
  96.  
  97.         public Item(Double x, Double y) {
  98.             super();
  99.             this.x = x;
  100.             this.y = y;
  101.         }
  102.  
  103.         @Override
  104.         public String toString() {
  105.             return "Item (" + x + ", " + y + ")";
  106.         }
  107.        
  108.     }
  109.  
  110.     /**
  111.      * @param args
  112.      * @throws Exception
  113.      */
  114.     public static void main(String[] args) throws Exception {
  115. //      BaseLinkedBlockingQueue<Item> queue = new OriginalLinkedBlockingQueue<Item>();
  116.         BaseLinkedBlockingQueue<Item> queue = new LinkedBlockingQueueWithoutHelp<Item>();
  117.         runWith(queue);
  118. //      System.gc();
  119.         System.out.println(queue);
  120.     }
  121.  
  122.     private static void runWith(BaseLinkedBlockingQueue<Item> queue) {
  123.         queue.enqueue(new Item(1.2, 2.2));
  124.         queue.enqueue(new Item(0.0, 0.0));
  125.        
  126.         Item head = queue.dequeue();
  127.         System.out.println(head);
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement