KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > eventmgr > EventThread


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.eventmgr;
13
14 /**
15  * This package private class is used for asynchronously dispatching events.
16  */

17
18 class EventThread extends Thread JavaDoc {
19     /**
20      * Queued is a nested top-level (non-member) class. This class
21      * represents the items which are placed on the asynch dispatch queue.
22      * This class is private.
23      */

24     private static class Queued {
25         /** listener list for this event */
26         final ListElement[] listeners;
27         /** dispatcher of this event */
28         final EventDispatcher dispatcher;
29         /** action for this event */
30         final int action;
31         /** object for this event */
32         final Object JavaDoc object;
33         /** next item in event queue */
34         Queued next;
35
36         /**
37          * Constructor for event queue item
38          *
39          * @param l Listener list for this event
40          * @param d Dispatcher for this event
41          * @param a Action for this event
42          * @param o Object for this event
43          */

44         Queued(ListElement[] l, EventDispatcher d, int a, Object JavaDoc o) {
45             listeners = l;
46             dispatcher = d;
47             action = a;
48             object = o;
49             next = null;
50         }
51     }
52
53     /** item at the head of the event queue */
54     private Queued head;
55     /** item at the tail of the event queue */
56     private Queued tail;
57     /** if false the thread must terminate */
58     private volatile boolean running;
59
60     /**
61      * Constructor for the event thread.
62      * @param threadName Name of the EventThread
63      */

64     EventThread(String JavaDoc threadName) {
65         super(threadName);
66         init();
67     }
68
69     /**
70      * Constructor for the event thread.
71      */

72     EventThread() {
73         super();
74         init();
75     }
76
77     private void init() {
78         running = true;
79         head = null;
80         tail = null;
81
82         setDaemon(true); /* Mark thread as daemon thread */
83     }
84
85     /**
86      * Stop thread.
87      */

88     void close() {
89         running = false;
90         interrupt();
91     }
92
93     /**
94      * This method pulls events from
95      * the queue and dispatches them.
96      */

97     public void run() {
98         try {
99             while (true) {
100                 Queued item = getNextEvent();
101                 if (item == null) {
102                     return;
103                 }
104                 EventManager.dispatchEvent(item.listeners, item.dispatcher, item.action, item.object);
105             }
106         }
107         catch (RuntimeException JavaDoc e) {
108             if (EventManager.DEBUG) {
109                 e.printStackTrace();
110             }
111             throw e;
112         }
113         catch (Error JavaDoc e) {
114             if (EventManager.DEBUG) {
115                 e.printStackTrace();
116             }
117             throw e;
118         }
119     }
120
121     /**
122      * This methods takes the input parameters and creates a Queued
123      * object and queues it.
124      * The thread is notified.
125      *
126      * @param l Listener list for this event
127      * @param d Dispatcher for this event
128      * @param a Action for this event
129      * @param o Object for this event
130      */

131     synchronized void postEvent(ListElement[] l, EventDispatcher d, int a, Object JavaDoc o) {
132         if (!isAlive()) { /* If the thread is not alive, throw an exception */
133             throw new IllegalStateException JavaDoc();
134         }
135         
136         Queued item = new Queued(l, d, a, o);
137
138         if (head == null) /* if the queue was empty */
139         {
140             head = item;
141             tail = item;
142         } else /* else add to end of queue */
143         {
144             tail.next = item;
145             tail = item;
146         }
147
148         notify();
149     }
150
151     /**
152      * This method is called by the thread to remove
153      * items from the queue so that they can be dispatched to their listeners.
154      * If the queue is empty, the thread waits.
155      *
156      * @return The Queued removed from the top of the queue or null
157      * if the thread has been requested to stop.
158      */

159     private synchronized Queued getNextEvent() {
160         while (running && (head == null)) {
161             try {
162                 wait();
163             }
164             catch (InterruptedException JavaDoc e) {
165             }
166         }
167
168         if (!running) { /* if we are stopping */
169             return null;
170         }
171
172         Queued item = head;
173         head = item.next;
174         if (head == null) {
175             tail = null;
176         }
177
178         return item;
179     }
180 }
Popular Tags