ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/Queue/Queue.java
Revision: 1.1
Committed: Thu Dec 28 00:58:43 2000 UTC (24 years ago) by tdb
Branch: MAIN
Log Message:
Starting point for a Queue that works in a multi-threaded environment.

Currently no comments, no tidying, or conformity to standards, but it works :-)

Further additions will include those just mentioned, plus support for a single
'producer' and multiple 'consumers', which is more precisely what we require.

File Contents

# User Rev Content
1 tdb 1.1 import java.util.LinkedList;
2     import java.util.NoSuchElementException;
3    
4     class Queue {
5    
6     public Queue() {
7     // Possible use this method instead ?
8     //_list = Collections.synchronizedList(new LinkedList(...));
9     _list = new LinkedList();
10     }
11    
12     public synchronized void add(Object o) {
13     int s = _list.size();
14     // add() does the same thing, but this ensures behaviour
15     _list.addLast(o);
16     if (s == 0) {
17     notifyAll();
18     }
19     _count++;
20     }
21    
22     public synchronized Object get() {
23     if (_list.size() == 0) {
24     try { wait(); } catch(Exception e) {}
25     }
26     Object o = null;
27     try {
28     o = _list.removeFirst();
29     }
30     catch (NoSuchElementException e) {
31     // no element... null already... so just leave
32     }
33     return o;
34     }
35    
36     public String status() {
37     String status = "";
38     status += "Current queue size = "+_list.size();
39     status += "\n";
40     status += "Queue-ometer = "+_count;
41     return status;
42     }
43    
44     private LinkedList _list;
45     private int _count;
46     }