FileDocCategorySizeDatePackage
EventDispatchThread.javaAPI DocAndroid 1.5 API4081Wed May 06 22:41:54 BST 2009java.awt

EventDispatchThread

public class EventDispatchThread extends Thread
author
Michael Danilov, Pavel Dolgov
version
$Revision$

Fields Summary
final Dispatcher
dispatcher
final Toolkit
toolkit
private org.apache.harmony.awt.wtk.NativeEventQueue
nativeQueue
protected volatile boolean
shutdownPending
Constructors Summary
EventDispatchThread(Toolkit toolkit, Dispatcher dispatcher)

        this.toolkit = toolkit;
        this.dispatcher = dispatcher;
        setName("AWT-EventDispatchThread"); //$NON-NLS-1$
        setDaemon(true);
    
Methods Summary
public voidrun()
Initialise and run the main event loop


                
    
       
        nativeQueue = toolkit.getNativeEventQueue();

        try {
            runModalLoop(null);
        } finally {
            toolkit.shutdownWatchdog.forceShutdown();
        }
    
voidrunModalLoop(ModalContext context)

        long lastPaintTime = System.currentTimeMillis();
        while (!shutdownPending && (context == null || context.isModalLoopRunning())) {
            try {
            EventQueue eventQueue = toolkit.getSystemEventQueueImpl();

            NativeEvent ne = nativeQueue.getNextEvent();
            if (ne != null) {
                dispatcher.onEvent(ne);
                MarkerEvent marker = new MarkerEvent(this, 0);
                eventQueue.postEvent(marker);
                for (AWTEvent ae = eventQueue.getNextEventNoWait(); 
                        (ae != null) && (ae != marker); 
                        ae = eventQueue.getNextEventNoWait()) {
                    eventQueue.dispatchEvent(ae);
                }
            } else {
                toolkit.shutdownWatchdog.setNativeQueueEmpty(true);
                AWTEvent ae = eventQueue.getNextEventNoWait();
                if (ae != null) {
                    eventQueue.dispatchEvent(ae);
                    long curTime = System.currentTimeMillis();
                    if (curTime - lastPaintTime > 10) {
                        toolkit.onQueueEmpty();
                        lastPaintTime = System.currentTimeMillis();
                    }
                } else {
                    toolkit.shutdownWatchdog.setAwtQueueEmpty(true);
                    toolkit.onQueueEmpty();
                    lastPaintTime = System.currentTimeMillis();
                    waitForAnyEvent();
                }
            }
            } catch (Throwable t) {
                // TODO: Exception handler should be implemented
                // t.printStackTrace();
            }
        }
    
voidshutdown()

        shutdownPending = true;
    
private voidwaitForAnyEvent()

        EventQueue eventQueue = toolkit.getSystemEventQueueImpl();
        if (!eventQueue.isEmpty() || !nativeQueue.isEmpty()) {
            return;
        }
        Object eventMonitor = nativeQueue.getEventMonitor();
        synchronized(eventMonitor) {
            try {
                eventMonitor.wait();
            } catch (InterruptedException e) {}
        }