home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jni / Poller / LinkedQueue.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.4 KB  |  200 lines

  1. /*
  2.  * @(#)LinkedQueue.java    1.4 01/12/03
  3.  *
  4.  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5.  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6.  */
  7.  
  8. /*
  9.   File: SLQ.java
  10.   Originally: LinkedQueue.java
  11.  
  12.   Originally written by Doug Lea and released into the public domain.
  13.   This may be used for any purposes whatsoever without acknowledgment.
  14.   Thanks for the assistance and support of Sun Microsystems Labs,
  15.   and everyone contributing, testing, and using this code.
  16.  
  17.   History:
  18.   Date       Who                What
  19.   11Jun1998  dl               Create public version
  20.   25aug1998  dl               added peek
  21.   10dec1998  dl               added isEmpty
  22.   10jun1999  bc               modified for isolated use
  23. */
  24.  
  25. // Original was in package EDU.oswego.cs.dl.util.concurrent;
  26.  
  27. /**
  28.  * A linked list based channel implementation,
  29.  * adapted from the TwoLockQueue class from CPJ.
  30.  * The algorithm avoids contention between puts
  31.  * and takes when the queue is not empty. 
  32.  * Normally a put and a take can proceed simultaneously. 
  33.  * (Although it does not allow multiple concurrent puts or takes.)
  34.  * This class tends to perform more efficently than
  35.  * other Channel implementations in producer/consumer
  36.  * applications.
  37.  * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
  38.  **/
  39.  
  40. public class LinkedQueue {
  41.  
  42.  
  43.   /** 
  44.    * Dummy header node of list. The first actual node, if it exists, is always 
  45.    * at head_.next. After each take, the old first node becomes the head.
  46.    **/
  47.   protected LinkedNode head_;         
  48.   protected int count_;
  49.   /**
  50.    * Helper monitor for managing access to last node, in case it is also first.
  51.    * last_ and waitingForTake_ ONLY used with synch on appendMonitor_
  52.    **/
  53.   protected final Object lastMonitor_ = new Object(); 
  54.  
  55.   /** 
  56.    * The last node of list. Put() appends to list, so modifies last_
  57.    **/
  58.   protected LinkedNode last_;         
  59.  
  60.   /**
  61.    * The number of threads waiting for a take.
  62.    * Notifications are provided in put only if greater than zero.
  63.    * The bookkeeping is worth it here since in reasonably balanced
  64.    * usages, the notifications will hardly ever be necessary, so
  65.    * the call overhead to notify can be eliminated.
  66.    **/
  67.   protected int waitingForTake_ = 0;  
  68.  
  69.   public LinkedQueue() {
  70.     head_ = new LinkedNode(null); 
  71.     last_ = head_;
  72.     count_ = 0;
  73.   }
  74.  
  75.   /** Main mechanics for put/offer **/
  76.   protected void insert(Object x) { 
  77.     synchronized(lastMonitor_) {
  78.       LinkedNode p = new LinkedNode(x);
  79.       last_.next = p;
  80.       last_ = p;
  81.       count_++;
  82.       if (count_ > 1000 && (count_ % 1000 == 0))
  83.     System.out.println("In Queue : " + count_);
  84.       if (waitingForTake_ > 0)
  85.         lastMonitor_.notify();
  86.     }
  87.   }
  88.  
  89.   /** Main mechanics for take/poll **/
  90.   protected synchronized Object extract() {
  91.     Object x = null;
  92.     LinkedNode first = head_.next;
  93.     if (first != null) {
  94.       x = first.value;
  95.       first.value = null;
  96.       head_ = first; 
  97.       count_ --;
  98.     }
  99.     return x;
  100.   }
  101.  
  102.  
  103.   public void put(Object x) throws InterruptedException {
  104.     if (x == null) throw new IllegalArgumentException();
  105.     if (Thread.interrupted()) throw new InterruptedException();
  106.     insert(x); 
  107.   }
  108.  
  109.   public boolean offer(Object x, long msecs) throws InterruptedException { 
  110.     if (x == null) throw new IllegalArgumentException();
  111.     if (Thread.interrupted()) throw new InterruptedException();
  112.     insert(x); 
  113.     return true;
  114.   }
  115.  
  116.   public Object take() throws InterruptedException {
  117.     if (Thread.interrupted()) throw new InterruptedException();
  118.     // try to extract. If fail, then enter wait-based retry loop
  119.     Object x = extract();
  120.     if (x != null)
  121.       return x;
  122.     else { 
  123.       synchronized(lastMonitor_) {
  124.         try {
  125.           ++waitingForTake_;
  126.           for (;;) {
  127.             x = extract();
  128.             if (x != null) {
  129.               --waitingForTake_;
  130.               return x;
  131.             }
  132.             else {
  133.               lastMonitor_.wait(); 
  134.             }
  135.           }
  136.         }
  137.         catch(InterruptedException ex) { 
  138.           --waitingForTake_; 
  139.           lastMonitor_.notify();
  140.           throw ex; 
  141.         }
  142.       }
  143.     }
  144.   }
  145.  
  146.   public synchronized Object peek() {
  147.     LinkedNode first = head_.next;
  148.     if (first != null) 
  149.       return first.value;
  150.     else 
  151.       return null;
  152.   }    
  153.  
  154.  
  155.   public synchronized boolean isEmpty() {
  156.     return head_.next == null;
  157.   }    
  158.  
  159.   public Object poll(long msecs) throws InterruptedException {
  160.     if (Thread.interrupted()) throw new InterruptedException();
  161.     Object x = extract();
  162.     if (x != null) 
  163.       return x;
  164.     else {
  165.       synchronized(lastMonitor_) {
  166.         try {
  167.           long waitTime = msecs;
  168.           long start = (msecs <= 0)? 0 : System.currentTimeMillis();
  169.           ++waitingForTake_;
  170.           for (;;) {
  171.             x = extract();
  172.             if (x != null || waitTime <= 0) {
  173.               --waitingForTake_;
  174.               return x;
  175.             }
  176.             else {
  177.               lastMonitor_.wait(waitTime); 
  178.               waitTime = msecs - (System.currentTimeMillis() - start);
  179.             }
  180.           }
  181.         }
  182.         catch(InterruptedException ex) { 
  183.           --waitingForTake_; 
  184.           lastMonitor_.notify();
  185.           throw ex; 
  186.         }
  187.       }
  188.     }
  189.   }
  190.  
  191.   class LinkedNode { 
  192.     Object value;
  193.     LinkedNode next = null;
  194.     LinkedNode(Object x) { value = x; }
  195.     LinkedNode(Object x, LinkedNode n) { value = x; next = n; }
  196.   }
  197. }
  198.  
  199.  
  200.