eventqueue.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 706 行 · 第 1/2 页
JAVA
706 行
} nextQueue = newEventQueue; AppContext appContext = AppContext.getAppContext(); if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) { appContext.put(AppContext.EVENT_QUEUE_KEY, newEventQueue); } } } /** * Stop dispatching events using this EventQueue instance. * Any pending events are transferred to the previous * EventQueue for processing by it. * * @exception if no previous push was made on this EventQueue. * @see java.awt.EventQueue#push */ protected void pop() throws EmptyStackException { if (debug) { System.out.println("EventQueue.pop(" + this + ")"); } // To prevent deadlock, we lock on the previous EventQueue before // this one. This uses the same locking order as everything else // in EventQueue.java, so deadlock isn't possible. EventQueue prev = previousQueue; synchronized ((prev != null) ? prev.proxy : this.proxy) { // 6370528 synchronized (this.proxy) { // 6370528 if (nextQueue != null) { nextQueue.pop(); return; } if (previousQueue == null) { throw new EmptyStackException(); } // Transfer all events back to previous EventQueue. previousQueue.nextQueue = null; while (peekEvent() != null) { try { previousQueue.postEventPrivate(getNextEvent()); } catch (InterruptedException ie) { if (debug) { System.err.println("interrupted pop:"); ie.printStackTrace(System.err); } } } AppContext appContext = AppContext.getAppContext(); if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) { appContext.put(AppContext.EVENT_QUEUE_KEY, previousQueue); } previousQueue = null; } } dispatchThread.stopDispatching(); // Must be done outside synchronized // block to avoid possible deadlock } /** * Returns true if the calling thread is the current AWT EventQueue's * dispatch thread. Use this call the ensure that a given * task is being executed (or not being) on the current AWT * EventDispatchThread. * * @return true if running on the current AWT EventQueue's dispatch thread. */ public static boolean isDispatchThread() { EventQueue eq = Toolkit.getEventQueue(); EventQueue next = eq.nextQueue; while (next != null) { eq = next; next = eq.nextQueue; } return (Thread.currentThread() == eq.dispatchThread); } /* * Get the EventDispatchThread for this EventQueue. */ final EventDispatchThread getDispatchThread() { return dispatchThread; } /* * Change the target of any pending KeyEvents because of a focus change. */ final void changeKeyEventFocus(Object newSource) { synchronized (this.proxy) { // 6370528 for (int i = 0; i < NUM_PRIORITIES; i++) { EventQueueItem q = queues[i].head; for (; q != null; q = q.next) { if (q.event instanceof KeyEvent) { q.event.setSource(newSource); } } } } } /* * Remove any pending events for the specified source object. * This method is normally called by the source's removeNotify method. */ final void removeSourceEvents(Object source) { synchronized (this.proxy) { // 6370528 for (int i = 0; i < NUM_PRIORITIES; i++) { EventQueueItem entry = queues[i].head; EventQueueItem prev = null; while (entry != null) { if (entry.event.getSource() == source) { if (entry.event instanceof SequencedEvent) { ((SequencedEvent)entry.event).dispose(); } if (entry.event instanceof SentEvent) { ((SentEvent)entry.event).dispose(); } if (prev == null) { queues[i].head = entry.next; } else { prev.next = entry.next; } } else { prev = entry; } entry = entry.next; } queues[i].tail = prev; } } } /* * Remove any pending events for the specified source object. * This method is normally called by the source's removeNotify method. */ final void removeEvents(Class type, int id) { synchronized (this.proxy) { // 6370528 for (int i = 0; i < NUM_PRIORITIES; i++) { EventQueueItem entry = queues[i].head; EventQueueItem prev = null; while (entry != null) { if (entry.event.getClass().equals(type) && entry.event.id == id) { if (prev == null) { queues[i].head = entry.next; } else { prev.next = entry.next; } } else { prev = entry; } entry = entry.next; } queues[i].tail = prev; } } } /** * Causes <i>runnable</i> to have its run() method called in the dispatch * thread of the EventQueue. This will happen after all pending events * are processed. * * @param runnable the Runnable whose run() method should be executed * synchronously on the EventQueue * @see #invokeAndWait * @since 1.2 */ public static void invokeLater(Runnable runnable) { Toolkit.getEventQueue().postEvent( new InvocationEvent(Toolkit.getDefaultToolkit(), runnable)); } /** * Causes <i>runnable</i> to have its run() method called in the dispatch * thread of the EventQueue. This will happen after all pending events * are processed. The call blocks until this has happened. This method * will throw an Error if called from the event dispatcher thread. * * @param runnable the Runnable whose run() method should be executed * synchronously on the EventQueue * @exception InterruptedException if another thread has * interrupted this thread * @exception InvocationTargetException if an exception is thrown * when running <i>runnable</i> * @see #invokeLater * @since 1.2 */ public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException { if (EventQueue.isDispatchThread()) { throw new Error("Cannot call invokeAndWait from the event dispatcher thread"); } class AWTInvocationLock {} Object lock = new AWTInvocationLock(); EventQueue queue = Toolkit.getEventQueue(); InvocationEvent event = new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock, true); synchronized (lock) { Toolkit.getEventQueue().postEvent(event); lock.wait(); } Exception eventException = event.getException(); if (eventException != null) { throw new InvocationTargetException(eventException); } } static void setCurrentEventAndMostRecentTime(AWTEvent e) { Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e); } private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) { synchronized(this.proxy) { // 6370528 if (Thread.currentThread() != dispatchThread) { return; } currentEvent = new WeakReference(e); // This series of 'instanceof' checks should be replaced with a // polymorphic type (for example, an interface which declares a // getWhen() method). However, this would require us to make such // a type public, or to place it in sun.awt. Both of these approaches // have been frowned upon. // // In tiger, we will probably give timestamps to all events, so this // will no longer be an issue. if (e instanceof InputEvent) { InputEvent ie = (InputEvent)e; mostRecentEventTime = ie.getWhen(); } else if (e instanceof ActionEvent) { ActionEvent ae = (ActionEvent)e; mostRecentEventTime = ae.getWhen(); } else if (e instanceof InvocationEvent) { InvocationEvent ie = (InvocationEvent)e; mostRecentEventTime = ie.getWhen(); } } } final void removeSourceEvents(Object source, boolean removeAllEvents) { synchronized (this.proxy) { // 6370528 for (int i = 0; i < NUM_PRIORITIES; i++) { EventQueueItem entry = queues[i].head; EventQueueItem prev = null; while (entry != null) { if ((entry.event.getSource() == source) && (removeAllEvents || ! ( entry.event instanceof SentEvent || entry.event instanceof SequencedEvent || entry.event instanceof FocusEvent || entry.event instanceof WindowEvent || entry.event instanceof KeyEvent))) { if (entry.event instanceof SequencedEvent) { ((SequencedEvent)entry.event).dispose(); } if (entry.event instanceof SentEvent) { ((SentEvent)entry.event).dispose(); } if (prev == null) { queues[i].head = entry.next; } else { prev.next = entry.next; } } else { prev = entry; } entry = entry.next; } queues[i].tail = prev; } } } public static long getMostRecentEventTime() { return Toolkit.getEventQueue().getMostRecentEventTimeImpl(); } private long getMostRecentEventTimeImpl() { synchronized(this.proxy) { // 6370528 return (Thread.currentThread() == dispatchThread) ? mostRecentEventTime : System.currentTimeMillis(); } } /** * Returns the the event currently being dispatched by the * <code>EventQueue</code> associated with the calling thread. This is * useful if a method needs access to the event, but was not designed to * receive a reference to it as an argument. Note that this method should * only be invoked from an application's event dispatching thread. If this * method is invoked from another thread, null will be returned. * * @return the event currently being dispatched, or null if this method is * invoked on a thread other than an event dispatching thread * @since 1.4 */ public static AWTEvent getCurrentEvent() { return Toolkit.getEventQueue().getCurrentEventImpl(); } private AWTEvent getCurrentEventImpl() { synchronized(this.proxy) { // 6370528 return (Thread.currentThread() == dispatchThread) ? ((AWTEvent)currentEvent.get()) : null; } } AWTEvent getNextEvent(int id) throws InterruptedException { return this.proxy.getNextEvent(id); // 6261461 } // 6261461 // EffectiveJava Pattern : Finalizer Guardian Idiom. private final Object finalizerGuardian = new Object() { protected void finalize() throws Throwable { try { EventQueue.this.dispatchThread.stopDispatching(); } finally { super.finalize(); } } }; // 6261461}/** * The Queue object holds pointers to the beginning and end of one internal * queue. An EventQueue object is composed of multiple internal Queues, one * for each priority supported by the EventQueue. All Events on a particular * internal Queue have identical priority. */class Queue { EventQueueItem head; EventQueueItem tail;}class EventQueueItem { AWTEvent event; int id; EventQueueItem next; EventQueueItem(AWTEvent evt) { event = evt; id = evt.getID(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?