FileDocCategorySizeDatePackage
WorkerThread.javaAPI DocExample2493Mon Aug 28 18:53:32 BST 2000com.imaginary.swing

WorkerThread

public abstract class WorkerThread extends Object

Fields Summary
private static com.imaginary.util.FifoStack
queue
private static Thread
worker
Constructors Summary
Methods Summary
public voidcomplete()
This method is called inside the Swing event queue. An implementation of this class does not need to implement this method unless it wants processing to occur specifically in the event queue.

    
public static voidinvokeWorker(com.imaginary.swing.WorkerThread wt)
Places a worker thread object onto the worker queue for execution in the worker thread. When the time is right, the run() method in the specified WorkerThread object will be run inside the worker thread. Upon completion, the complete() method will then be executed inside the event queue.

param
wt the worker to be executed inside the worker thread


                                                                    
         
        synchronized( queue ) {
            queue.push(wt);
            if( worker == null ) {
                worker = new Thread() {
                        public void run() {
                            runThread();
                        }
                    };
                worker.setDaemon(true);
                worker.setPriority(Thread.NORM_PRIORITY);
                worker.setName("Worker Queue");
                worker.start();
            }
        }
    
public abstract voidrun()
Implementors must implement this method to specify the processing that should occur in the worker thread.

private static voidrunThread()

        while( true ) {
            final WorkerThread wt;
            
            synchronized( queue ) {
                if( queue.isEmpty() ) {
                    worker = null;
                    return;
                }
                wt = (WorkerThread)queue.pop();
            }
            try {
                Runnable r;
                
                wt.run();
                r = new Runnable() {
                        public void run() {
                            wt.complete();
                        }
                    };
                // place a call to the complete() method in the event queue
                SwingUtilities.invokeLater(r);
            }
            catch( Exception e ) {
                e.printStackTrace();
            }
        }