1 11 12 package org.eclipse.osgi.framework.eventmgr; 13 14 17 18 class EventThread extends Thread { 19 24 private static class Queued { 25 26 final ListElement[] listeners; 27 28 final EventDispatcher dispatcher; 29 30 final int action; 31 32 final Object object; 33 34 Queued next; 35 36 44 Queued(ListElement[] l, EventDispatcher d, int a, Object o) { 45 listeners = l; 46 dispatcher = d; 47 action = a; 48 object = o; 49 next = null; 50 } 51 } 52 53 54 private Queued head; 55 56 private Queued tail; 57 58 private volatile boolean running; 59 60 64 EventThread(String threadName) { 65 super(threadName); 66 init(); 67 } 68 69 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); 83 } 84 85 88 void close() { 89 running = false; 90 interrupt(); 91 } 92 93 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 e) { 108 if (EventManager.DEBUG) { 109 e.printStackTrace(); 110 } 111 throw e; 112 } 113 catch (Error e) { 114 if (EventManager.DEBUG) { 115 e.printStackTrace(); 116 } 117 throw e; 118 } 119 } 120 121 131 synchronized void postEvent(ListElement[] l, EventDispatcher d, int a, Object o) { 132 if (!isAlive()) { 133 throw new IllegalStateException (); 134 } 135 136 Queued item = new Queued(l, d, a, o); 137 138 if (head == null) 139 { 140 head = item; 141 tail = item; 142 } else 143 { 144 tail.next = item; 145 tail = item; 146 } 147 148 notify(); 149 } 150 151 159 private synchronized Queued getNextEvent() { 160 while (running && (head == null)) { 161 try { 162 wait(); 163 } 164 catch (InterruptedException e) { 165 } 166 } 167 168 if (!running) { 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 |