KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > util > MessageQueue


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.util;
15
16 /**
17  * MessageQueue is a thread-safe queue suitable for inter-thread communication
18  */

19 public class MessageQueue
20 {
21   private boolean m_deactivated = false;
22   private Element m_head = null;
23   private Element m_tail = null;
24
25
26   private static class Element
27   {
28     public Object JavaDoc value;
29     public Element next;
30
31     public Element(Object JavaDoc val)
32     {
33       value = val;
34       next = null;
35     }
36   }
37
38
39   public MessageQueue()
40   {
41   }
42
43
44   public synchronized void deactivate()
45   {
46     m_deactivated = true;
47     notifyAll();
48   }
49
50
51   public synchronized boolean getDeactivated()
52   {
53     return m_deactivated;
54   }
55
56
57   public synchronized boolean getEmpty()
58   {
59     return (m_head == null);
60   }
61
62
63   public synchronized void enqueue(Object JavaDoc value)
64   {
65     Element elem = new Element(value);
66     if (m_tail == null)
67       m_head = m_tail = elem;
68     else {
69       m_tail.next = elem;
70       m_tail = elem;
71     }
72
73       // notify any waiting threads that a value is available
74
notifyAll();
75   }
76
77
78   public synchronized Object JavaDoc dequeue()
79   {
80     Object JavaDoc result = null;
81
82     while (result == null && ! m_deactivated) {
83         // if there are no elements available, then block until there are
84
if (m_head == null) {
85         try {
86           wait();
87         }
88         catch (InterruptedException JavaDoc e) {
89         }
90       }
91       else {
92           // otherwise remove the first element on the queue
93
result = m_head.value;
94         m_head = m_head.next;
95         if (m_head == null)
96           m_tail = null;
97       }
98     }
99
100     return result;
101   }
102
103
104   /******************* comment out to enable main()
105
106   public static void main(String[] args)
107   {
108     MessageQueue queue = new MessageQueue();
109
110       // create a few reader threads
111     for (int i = 1; i <= 3; i++) {
112       Thread t = new Reader(i, queue);
113       t.setPriority(Thread.currentThread().getPriority() + 1);
114       t.start();
115     }
116
117     for (int i = 0; i < 10; i++) {
118       Integer value = new Integer(i);
119       System.out.println("Main - enqueuing " + value);
120       queue.enqueue(value);
121     }
122
123       // wait for queue to empty
124     while (! queue.getEmpty()) {
125       try {
126         Thread.sleep(500);
127       }
128       catch (InterruptedException e) {
129       }
130     }
131
132       // shut off the queue, which should stop all threads
133     queue.deactivate();
134   }
135
136
137   protected static class Reader extends Thread
138   {
139     private int m_id;
140     private MessageQueue m_queue;
141
142     public Reader(int id, MessageQueue queue)
143     {
144       m_id = id;
145       m_queue = queue;
146       System.out.println("Thread " + m_id + " starting up...");
147     }
148
149     public void run()
150     {
151       Object value;
152       while ((value = m_queue.dequeue()) != null) {
153         Integer i = (Integer)value;
154         System.out.println("Thread " + m_id + " received " + i);
155         try {
156           Thread.sleep(500);
157         }
158         catch (InterruptedException e) {
159         }
160       }
161       System.out.println("Thread " + m_id + " done...");
162     }
163   }
164
165   /******************* comment out to enable main() */

166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Popular Tags