package com.eddic.test;import java.util.Date;import com.eddic.adt.Queue;
import com.eddic.adtexception.QueueException;public class QueueTest {

public static void main(String args[]) {
Queue printer = new Queue();
printer.enqueue(new PrintJob("刘备", new Date()));
printer.enqueue(new PrintJob("关云长", new Date()));
try {
Thread.sleep(2000); //sleep放在这里会出现小小的问题,未弄懂

while(!printer.isEmpty()){
System.out.println("Printing starts: " + (new Date()));
System.out.println("\tJob: " + printer.dequeue());
}

System.out.println("Printing starts: " + (new Date()));
System.out.println("\tJob: " + printer.dequeue());
}
catch(QueueException qe) {
System.err.println(qe);
}
catch(InterruptedException ie) {
System.err.println(ie);
}
}
}打印出来的顺序和理论上的不一样,请教大家,可否简单讲一下原因 
注:Queue为自己写的队列类,QueueException为简单的队列异常类,PrintJob类只有name、Date属性,以及重写了 
toString()方法而已 

解决方案 »

  1.   

    可能是你的queue写的有问题吧。试试系统的LinkedBlockingQueue
      

  2.   

    不知你的程序想做什么。
    1)你程序中放了sleep(2000)(想做什么呢?),那么,2秒时间一到,立即发出InterruptedException,你的程序就停止了,那么你后边的那个while()代码就没机会执行了。
    2)while()代码是将queue中元素按进行队列的次序进行输出。while()全部输出完成后,那么while()之后的那个再一次输出的代码又是干什么的呢?
    一句话:不知道你这个代码想要做的意图是什么
    你究竟想要做什么事情?
      

  3.   


    昨天我也是给一个同学解决了一个打印顺序的问题,关于递归栈的。你好好看看你的queue类,可能写成了栈呢
      

  4.   

    你的程序错误很多,还能运行出结果吗?
    1.你的类QueueTest 并没有设为线程,可以implements Runnable,然后重写run 函数
    2.你让主函数休眠2秒干嘛啊,你的程序很乱。不是改一两处就能实现的
    多看点基础知识,参考一下别人写的程序。
      

  5.   


    同意2楼的说法我感觉你的睡眠应该单独放出来丢到while里面去执行吧?不然2秒一次,那个while该怎么去执行呢?
    然后,你另外一个类也没贴上去
      

  6.   


    package com.eddic.adt;
    import com.eddic.adtexception.QueueException;public class Queue { private Node head;
    private Node tail;

    public Queue() {
    head = tail = null;
    }

    public void enqueue(Object obj) {
    Node node = new Node(obj);
    if(head == null)
    head = node;
    else
    tail.next = node;
    tail = node;
    }

    public Object dequeue() throws QueueException {
    if(head == null)
    throw new QueueException("从空队列删除元素");
    else{
    Object data = head.data;
    head = head.next;
    if(head == null)
    tail = null;
    return data;
    }
    }

    public Object peek() throws QueueException {
    if(head == null)
    throw new QueueException("在空队列中查看元素");
    else
    return head.data;
    }

    public boolean isEmpty() {
    return (head == null);
    }
    }这个就是Queue类了,我只是看书上的,现在测试一下!
    2楼说的(1)我不觉得是对的哟~~ 至于(2)我也知道这样子没啥意义,不过只为测试,看下结果
    下面是QueueException类package com.eddic.adtexception;public class QueueException extends Exception {

    public QueueException() {
    super("队列出现异常");
    }

    public QueueException(String msg) {
    super(msg);
    }
    }
    运行的结果为:(有时会不一样)
    Printing starts: Sun Mar 27 19:45:22 CST 2011
    com.eddic.adtexception.QueueException: 从空队列删除元素
    Job: 刘备(Sun Mar 27 19:45:20 CST 2011)
    Printing starts: Sun Mar 27 19:45:22 CST 2011
    Job: 关云长(Sun Mar 27 19:45:20 CST 2011)
    Printing starts: Sun Mar 27 19:45:22 CST 2011
      

  7.   


    package com.eddic.test;import java.util.Date;public class PrintJob {

    private String name;
    private Date date;

    public PrintJob(String name, Date date) {
    this.name = name;
    this.date = date;
    }

    public String toString() {
    return (name + "(" + date + ")");
    }
    }
    类都贴出来了,我知道这比较滑稽,希望大家别鄙视哦~~望不吝赐教!
      

  8.   

    书上的例子,我打出来测试一下!按照我的理解,sleep2秒后,应该又回到主线程继续执行下去,即执行while,先打印的应该是队列里边的元素,然后结束while,再打印就会抛出空队列异常!
    但是实践结果却是乱了套,看下我发的结果,不仅是先抛出异常,而且打印的顺序也错了——不应该是Printing starts先的吗
    还请大哥指点指点,或是在你的机器上试跑一下。小弟先谢啦