KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > concurrent > FIFOWaitQueue


1 package org.apache.beehive.netui.util.internal.concurrent;
2
3 import java.util.*;
4
5 /**
6  * Simple linked list queue used in FIFOSemaphore.
7  * Methods are not synchronized; they depend on synch of callers.
8  * Must be public, since it is used by Semaphore (outside this package).
9  * NOTE: this class is NOT present in java.util.concurrent.
10  **/

11
12 class FIFOWaitQueue extends WaitQueue implements java.io.Serializable JavaDoc {
13     protected transient WaitNode head_ = null;
14     protected transient WaitNode tail_ = null;
15
16     public FIFOWaitQueue() {}
17
18     public void insert(WaitNode w) {
19         if (tail_ == null)
20             head_ = tail_ = w;
21         else {
22             tail_.next = w;
23             tail_ = w;
24         }
25     }
26
27     public WaitNode extract() {
28         if (head_ == null)
29             return null;
30         else {
31             WaitNode w = head_;
32             head_ = w.next;
33             if (head_ == null)
34                 tail_ = null;
35             w.next = null;
36             return w;
37         }
38     }
39
40     public boolean hasNodes() {
41         return head_ != null;
42     }
43
44     public int getLength() {
45         int count = 0;
46         WaitNode node = head_;
47         while (node != null) {
48             if (node.waiting) count++;
49             node = node.next;
50         }
51         return count;
52     }
53
54     public Collection getWaitingThreads() {
55         List list = new ArrayList();
56         int count = 0;
57         WaitNode node = head_;
58         while (node != null) {
59             if (node.waiting) list.add(node.owner);
60             node = node.next;
61         }
62         return list;
63     }
64
65     public boolean isWaiting(Thread JavaDoc thread) {
66         if (thread == null) throw new NullPointerException JavaDoc();
67         for (WaitNode node = head_; node != null; node = node.next) {
68             if (node.waiting && node.owner == thread) return true;
69         }
70         return false;
71     }
72
73 }
74
Popular Tags