foxtrot
Class Worker

java.lang.Object
  extended by foxtrot.Worker

public class Worker
extends Object

The class that execute time-consuming Tasks and Jobs.
It is normally used in event listeners that must execute time-consuming operations without freezing the Swing GUI.
Usage example (simplified from the Foxtrot examples):

 JButton button = new JButton("Take a nap!");
 button.addActionListener(new ActionListener()
 {
    public void actionPerformed(ActionEvent e)
    {
       try
       {
          Worker.post(new Task()
          {
             public Object run() throws Exception
             {
                 Thread.sleep(10000);
                 return null;
             }
          });
       }
       catch (Exception ignored) {}
    }
 });
 
While normally not necessary, it is possible to customize the two core components of this class, the EventPump and the WorkerThread via the corrispondent setter methods.
This class uses by default a WorkerThread that enqueues Tasks; Tasks will be executed one after the other. This is the behavior needed by most applications that want to rely on a synchronous model. For an alternative behavior, see ConcurrentWorker

Version:
$Revision: 255 $

Method Summary
static EventPump getEventPump()
          Returns the EventPump used to pump events from the AWT Event Queue.
static WorkerThread getWorkerThread()
          Returns the WorkerThread used to run Tasks subclasses in a thread that is not the Event Dispatch Thread.
static Object post(Job job)
          Enqueues the given Job to be executed in the worker thread.
static Object post(Task task)
          Enqueues the given Task to be executed by the WorkerThread, while dequeueing AWT events.
static void setEventPump(EventPump eventPump)
          Sets the EventPump to be used to pump events from the AWT Event Queue.
static void setWorkerThread(WorkerThread workerThread)
          Sets the WorkerThread used to run Tasks subclasses in a thread that is not the Event Dispatch Thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getWorkerThread

public static WorkerThread getWorkerThread()
Returns the WorkerThread used to run Tasks subclasses in a thread that is not the Event Dispatch Thread. By default, the WorkerThread will enqueue the Tasks and execute them one after the other.

See Also:
setWorkerThread(foxtrot.WorkerThread)

setWorkerThread

public static void setWorkerThread(WorkerThread workerThread)
Sets the WorkerThread used to run Tasks subclasses in a thread that is not the Event Dispatch Thread.

Throws:
IllegalArgumentException - If workerThread is null
See Also:
getWorkerThread()

getEventPump

public static EventPump getEventPump()
Returns the EventPump used to pump events from the AWT Event Queue.
If no calls to setEventPump(foxtrot.EventPump) have been made a default pump is returned; this default pump is obtained by instantiating the suitable pump for the Java version that is running the code.

See Also:
setEventPump(foxtrot.EventPump)

setEventPump

public static void setEventPump(EventPump eventPump)
Sets the EventPump to be used to pump events from the AWT Event Queue.
After calling this method, subsequent invocation of post(Task) or post(Job) will use the newly installed EventPump. Use with care, since implementing correcly a new EventPump is not easy. Foxtrot's default EventPumps are normally sufficient.

Throws:
IllegalArgumentException - If eventPump is null
See Also:
getEventPump()

post

public static Object post(Task task)
                   throws Exception
Enqueues the given Task to be executed by the WorkerThread, while dequeueing AWT events.
If this method is called from the Event Dispatch Thread, it blocks until the task has been executed, either by finishing normally or throwing an exception.
If this method is called from a worker thread, it executes the new Task immediately in the same thread and then returns the control to the calling Task.
While Tasks are executed, AWT events are dequeued from the AWT Event Queue; even in case of AWT events that throw RuntimeExceptions or Errors, this method will not return until the first Task (posted from the Event Dispatch Thread) is finished.

Throws:
IllegalStateException - if is not called from the Event Dispatch Thread nor from a worker thread.
Exception
See Also:
post(Job job)

post

public static Object post(Job job)
Enqueues the given Job to be executed in the worker thread.
This method behaves exactly like post(Task task), but it does not throw checked exceptions.

Throws:
IllegalStateException - if is not called from the Event Dispatch Thread nor from another Job or Task.
See Also:
post(Task)


Copyright © 2011 KnowledgePlaza. All Rights Reserved.