01: package org.apache.beehive.netui.util.internal.concurrent;
02:
03: import java.util.*;
04:
05: /**
06: * Simple linked list queue used in FIFOSemaphore.
07: * Methods are not synchronized; they depend on synch of callers.
08: * Must be public, since it is used by Semaphore (outside this package).
09: * NOTE: this class is NOT present in java.util.concurrent.
10: **/
11:
12: class FIFOWaitQueue extends WaitQueue implements java.io.Serializable {
13: protected transient WaitNode head_ = null;
14: protected transient WaitNode tail_ = null;
15:
16: public FIFOWaitQueue() {
17: }
18:
19: public void insert(WaitNode w) {
20: if (tail_ == null)
21: head_ = tail_ = w;
22: else {
23: tail_.next = w;
24: tail_ = w;
25: }
26: }
27:
28: public WaitNode extract() {
29: if (head_ == null)
30: return null;
31: else {
32: WaitNode w = head_;
33: head_ = w.next;
34: if (head_ == null)
35: tail_ = null;
36: w.next = null;
37: return w;
38: }
39: }
40:
41: public boolean hasNodes() {
42: return head_ != null;
43: }
44:
45: public int getLength() {
46: int count = 0;
47: WaitNode node = head_;
48: while (node != null) {
49: if (node.waiting)
50: count++;
51: node = node.next;
52: }
53: return count;
54: }
55:
56: public Collection getWaitingThreads() {
57: List list = new ArrayList();
58: int count = 0;
59: WaitNode node = head_;
60: while (node != null) {
61: if (node.waiting)
62: list.add(node.owner);
63: node = node.next;
64: }
65: return list;
66: }
67:
68: public boolean isWaiting(Thread thread) {
69: if (thread == null)
70: throw new NullPointerException();
71: for (WaitNode node = head_; node != null; node = node.next) {
72: if (node.waiting && node.owner == thread)
73: return true;
74: }
75: return false;
76: }
77:
78: }
|