⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unbiasedqueue.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  Copyright (c) 2001-2002 Sun Microsystems, Inc.  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in
 *  the documentation and/or other materials provided with the
 *  distribution.
 *
 *  3. The end-user documentation included with the redistribution,
 *  if any, must include the following acknowledgment:
 *  "This product includes software developed by the
 *  Sun Microsystems, Inc. for Project JXTA."
 *  Alternately, this acknowledgment may appear in the software itself,
 *  if and wherever such third-party acknowledgments normally appear.
 *
 *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
 *  not be used to endorse or promote products derived from this
 *  software without prior written permission. For written
 *  permission, please contact Project JXTA at http://www.jxta.org.
 *
 *  5. Products derived from this software may not be called "JXTA",
 *  nor may "JXTA" appear in their name, without prior written
 *  permission of Sun.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 *  ====================================================================
 *
 *  This software consists of voluntary contributions made by many
 *  individuals on behalf of Project JXTA.  For more
 *  information on Project JXTA, please see
 *  <http://www.jxta.org/>.
 *
 *  This license is based on the BSD license adopted by the Apache Foundation.
 *
 *  $Id: UnbiasedQueue.java,v 1.1 2002/05/16 05:56:14 bondolo Exp $
 */

package net.jxta.impl.util;

import org.apache.log4j.Category;
import org.apache.log4j.Priority;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import net.jxta.impl.util.TimeUtils;

/**
 *  A generic queue class. This queue is explicitly NOT a synchronized queue.
 *
 *  <p/>FIXME 20020511  bondolo@jxta.org    This could be more efficient with a
 *  circular queue implementation, but its a pain to write since we allow the
 *  queue to be resizable.
 *
 *  <p/>FIXME 20020511  bondolo@jxta.org    Exercise for the reader: Extend this
 *  class so that it does both LIFO and FIFO.
 *
 * @since      JXTA 1.0
 */

public class UnbiasedQueue  {
  
  /**
   *  Log4J
   **/
  private static final Category LOG = Category.getInstance(UnbiasedQueue.class.getName());
  
  /**
   *  Default number of queue elements.
   **/
  protected static final int DEFAULT_MAX_OBJECTS = 100;
  
  /**
   *  Default object dropping behaviour
   **/
  protected static final boolean DROP_OLDEST_OBJECT = true;
  
  /**
   *  Number of milliseconds between notifications that objects are being dropped.
   **/
  protected static final long DROPPED_OBJECT_WARNING_INTERVAL = 10000L;
  
  /**
   *  Contains the objects we currently have queued.
   **/
  protected List queue = null;
  
  /**
   *  The maximum number of objects we will hold in the queue at one time.
   **/
  protected int maxObjects;
  
  /**
   *  If true the queue is being closed and is currently in the process of
   *  being flushed. All new "push" requests will be refused.
   **/
  protected volatile boolean closeFlag = false;
  
  /**
   *  When we need to drop objects, drop the oldest obj.
   **/
  protected boolean dropOldestObject = true;
  
  /** total number of objects which have been enqueued into this queue **/
  protected long numEnqueued = 0;
  
  /** sum of queue sizes at enqueue time. **/
  protected long sumOfQueueSizesEnqueue = 0;
  
  /** total number of objects which have been dequeued from this queue **/
  protected long numDequeued = 0;
  
  /** sum of queue sizes at dequeue time. **/
  protected long sumOfQueueSizesDequeue = 0;
  
  /** the number of objects we have dropped since we began working. **/
  protected long numDropped = 0;
  
  /** absolute time in millis when it will be ok to display a obj about
   * dropping objects. We throttle this so that there is a chance to do work
   * rather than just always spewing warnings.
   */
  protected long nextDroppedWarn = 0L;
  
  /**
   *  An inner class for wrapping arbitrary queues with synchronization.
   *
   **/
  protected static class SynchronizedQueue extends UnbiasedQueue {
    UnbiasedQueue innerqueue;
    
    public SynchronizedQueue( UnbiasedQueue queue ) {
      innerqueue = queue;
    }
    
    public synchronized void clear() {
      innerqueue.clear();
    }
    
    public synchronized boolean push( Object obj, long timeout ) throws InterruptedException {
      return innerqueue.push( obj, timeout );
    }
    
    public synchronized Object pop() {
      return innerqueue.pop();
    }
    
    public synchronized Object pop( long timeout ) throws InterruptedException {
      return innerqueue.pop( timeout );
    }
    
    public synchronized Object [] popMulti( int maxObjs ) {
      return innerqueue.popMulti(maxObjs);
    }
    
    public synchronized int getMaxQueueSize() {
      return innerqueue.getMaxQueueSize();
    }
    
    public synchronized void setMaxQueueSize( int maxObjs ) {
      innerqueue.setMaxQueueSize( maxObjs );
    }
    
    public synchronized int getCurrentInQueue() {
      return innerqueue.getCurrentInQueue();
    }
    
    public synchronized long getNumEnqueued() {
      return innerqueue.getNumEnqueued();
    }
    
    public synchronized double getAvgInQueueAtEnqueue() {
      return innerqueue.getAvgInQueueAtEnqueue();
    }
    
    public synchronized long getNumDequeued() {
      return innerqueue.getNumDequeued();
    }
    
    public synchronized double getAvgInQueueAtDequeue() {
      return innerqueue.getAvgInQueueAtDequeue();
    }
    
    public synchronized long getNumDropped() {
      return innerqueue.getNumDropped();
    }
  }
  
  /**
   *  Returns a synchronized (thread-safe) list backed by the specified queue.
   *  Most UnbiasedQueue subclasses are either unsynchronized or internally
   *  synchronized. If you need to do any atomic operations upon
   *  UnbiasedQueues (or subclasses) then this method should be used to
   *  "wrap" the queue with synchronization.
   *
   *  <p/>In order to guarantee serial access, it is critical that all access
   * to the backing queue is accomplished through the returned queue.
   *
   *  @param queue the queue to be "wrapped" in a synchronized queue.
   **/
  public static UnbiasedQueue synchronizedQueue( UnbiasedQueue queue ) {
    return new SynchronizedQueue( queue );
  }
  
  /**
   * Default constructor. 100 element LIFO queue which drops oldest element
   * when full.
   */
  public UnbiasedQueue() {
    this( DEFAULT_MAX_OBJECTS, DROP_OLDEST_OBJECT );
  }
  
  /**
   * Full featured constructor for creating a new ConsumerBiasedQueue.
   *
   * @param size    Queue will be not grow larger than this size. Use
   *    Integer.MAX_VALUE for "unbounded" queue size.
   * @param dropOldest  Controls behaviour of element insertion when the queue is
   * full. If "true" and the queue is full upon a push operation then the
   * oldest element will be dropped to be replaced with the element currently
   * being pushed. If "false" then then newest item will be dropped.
   */
  public UnbiasedQueue(int size, boolean dropOldest) {
    this( size, dropOldest, new ArrayList() );
  }
  
  /**
   * Full featured constructor for creating a new ConsumerBiasedQueue.
   *
   * @param size    Queue will be not grow larger than this size. Use
   *    Integer.MAX_VALUE for "unbounded" queue size.
   * @param dropOldest  Controls behaviour of element insertion when the queue is
   * full. If "true" and the queue is full upon a push operation then the
   * oldest element will be dropped to be replaced with the element currently
   * being pushed. If "false" then then newest item will be dropped.
   *  @param queue    the List class instance to use.
   **/
  protected UnbiasedQueue(int size, boolean dropOldest, List queue ) {
    if( size <= 0 )
      throw new IllegalArgumentException( "size must be > 0" );
    
    if( null == queue )
      throw new IllegalArgumentException( "queue must be non-null" );
    
    maxObjects = size;
    this.queue = queue;
    closeFlag = false;
    
    dropOldestObject = dropOldest;
  }
  
  /**
   *  Atomically return whether or not this queue has been closed. Closed
   *  queues will not accept "push" requests, but elements will still be
   *  returned with "pop".
   *
   *  @return boolean indicating whether this queue has been closed.
   **/
  public boolean isClosed() {
    return closeFlag; // closeFlag is volatile.
  }
  
  /**
   * Close the queue. This will prevent any further objects from being enqueued.
   **/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -