[1526] in java-interest
Queue class
daemon@ATHENA.MIT.EDU (Simon Spero)
Wed Sep 6 23:59:52 1995
Date: Wed, 6 Sep 1995 18:03:04 -0700 (PDT)
From: Simon Spero <ses@tipper.oit.unc.edu>
To: Java <java-interest@java.Eng.Sun.COM>
Here's the queue class I mentioned yesterday. It's probably suffered
severe bit-rot, but it's an example of how you can write your own list class.
Simon
/*
$Header: /users/ses/java/test/scp/RCS/Queue.java,v 1.1 1995/09/07 00:46:01 ses Exp ses $
*/
import java.*;
/**
* QueueNode is a utility class used by Queue, a doubly linked list
* Users should not instantiate this class directly
* @version $Version:$
* @author Simon Spero (ses@unc.edu)
*/
class QueueNode {
public QueueNode next = this;
public QueueNode prev = this;
public Object o = null;
/**
Create a queue node containing Object a, and link it in to the list
immediately after QueueNode p
*/
QueueNode(Object a, QueueNode p) {
prev = p; next = p.next;
next.prev = this; prev.next = this;
o = a;
}
/**
Create an empty QueueNode linked to itself
*/
QueueNode() {next = this ; prev = this ; o = null;}
/**
remove this queuenode from a list
*/
void unlink() {
prev.next = next;
next.prev = prev;
}
}
/**
Queue is a doubly linked list
*/
class Queue {
/**
theQueue is used to hold the real list data
*/
QueueNode theQueue = new QueueNode();
/**
add an object onto the end of the list and wake up anybody sleeping on
the queue
*/
public synchronized void push(Object o) {
new QueueNode(o,theQueue.prev);
notifyAll();
}
/**
add an object to the end of the list, wake up any sleepers, and yield
control to any other threads waiting to run.
*/
public void append(Object o) {
push(o);
Thread.yield();
}
/**
Add an object to the start of the list
*/
public synchronized void prepend(Object o) {
new QueueNode(o,theQueue);
}
/**
Test to see if the queue is empty
*/
public synchronized boolean isEmpty() {
return theQueue.next == theQueue;
}
/**
Remove the first item from the list, and return it.
If the list is empty, wait until something is added
*/
public synchronized Object pop() {
while(true) {
if(isEmpty()) {
System.out.println("sleeping");
wait();
} else {
Object tmp = theQueue.next.o;
theQueue.next.unlink();
return tmp;
}
}
}
public String toString() {
String tmp = "";
for(QueueNode q = theQueue.next; q != theQueue ; q = q.next) {
tmp += q.o.toString();
tmp += " ";
}
return tmp;
}
}
/*
The remainder of the file is an example of the list used as a message
queue between two threads.
*/
class reader extends Thread {
Queue q;
reader(Queue a) {q = a;}
public void run() {
while(true) {
Object o = q.pop();
if(o == null) {
return;
} else {
System.out.println(o);
}
}
}
}
class QueueTest {
static public void main(String argv[]) {
Object o = new Object();
Queue q = new Queue();
reader r = new reader(q);
r.start();
q.append("a");
System.out.println("hello");
q.append("b");
q.append("c");
q.append(null);
}
}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com