建议用LinkedList做消息队列,不要用数组。

解决方案 »

  1.   

    建议用vector作队列
    可以这样:public class Queue extends java.util.Vector { 
    public Queue() { 
    super(); 

    public synchronized void enq(Object x) { 
    super.addElement(x); 

    public synchronized Object deq() { 
    /* 队列若为空,引发EmptyQueueException异常 */ 
    if( this.empty() ) 
    throw new EmptyQueueException(); 
    Object x = super.elementAt(0); 
    super.removeElementAt(0); 
    return x; 

    public synchronized Object front() { 
    if( this.empty() ) 
    throw new EmptyQueueException(); 
    return super.elementAt(0); 

    public boolean empty() { 
    return super.isEmpty(); 

    public synchronized void clear() { 
    super.removeAllElements(); 

    public int search(Object x) { 
    return super.indexOf(x); 

    } public class EmptyQueueException extends java.lang.RuntimeException { 
    public EmptyQueueException() { 
    super();