| 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 |
|
|
} |