| Foxtrot APIThe Foxtrot API is very small and simple, and consists of 3 main classes: 
class foxtrot.Workerclass foxtrot.Taskclass foxtrot.Job From Foxtrot 2.x, the API has been extended to allow customization of the part that handles event pumping
and of the part that handles execution of Tasks andJobs in a worker thread, via
the following classes: 
interface foxtrot.EventPumpinterface foxtrot.WorkerThreadclass foxtrot.AbstractWorkerThread Normally users do not need to deal with these classes to use Foxtrot in their Swing applications, since
Foxtrot will configure itself with the most suitable implementations; however, if
a specific customization of the event pumping mechanism or of the worker thread mechanism is needed, the APIs
provided by these classes allow fine grained control on Foxtrot's behavior. Foxtrot API DetailsThe Workerclass is used to postTasks orJobs that will be executed
in the Foxtrot Worker Thread. The Taskclass is subclassed by the user to perform heavy tasks that throw checked exceptions. The Jobclass, conversely, is subclassed by the user to perform heavy tasks that do not throw
checked exceptions, but only RuntimeExceptions (or Errors). The Workerclass has the following 2 public methods that can be used to postTasks orJobs: 
public static Object post(Task task) throws Exceptionpublic static Object post(Job job) The Taskclass has a single abstract method that must be implemented by the user, with the
time-consuming code that may throw checked exceptions: 
public abstract Object run() throws Exception The Jobclass, conversely, has a single abstract method that must be implemented by the user,
with the time-consuming code that does not throw checked exceptions: 
public abstract Object run() The exceptions or errors thrown inside the Task.run()orJob.run()methods are
re-thrown by the corrispondentWorker.post(...)method as is, i.e. without being wrapped into,
for example, an InvocationTargetException. The usage is very simple; here's an example with the Jobclass: 
Worker.post(new Job()
{
   public Object run()
   {
      // Here write the time-consuming code
      // that does not throw checked exceptions
   }
});
and here's an example with the Taskclass: 
try
{
   Worker.post(new Task()
   {
      public Object run() throws Exception
      {
         // Here write the time-consuming code
         // that may throw checked exceptions
      }
   });
}
catch (Exception x)
{
   // Handle the exception thrown by the Task
}
It is possible to narrow the throws clause of the Taskclass, but unfortunately not the one of
theWorkerclass.So, when using the
 Worker.post(Task task)method, you have to surround it in atry...catch(Exception x)block (unless the method that containsWorker.post(Task task)throwsExceptionitself). 
try
{
   Worker.post(new Task()
   {
      public Object run() throws FileNotFoundException
      {
         // Here write the time-consuming code
         // that accesses the file system
      }
   });
}
catch (FileNotFoundException x)
{
   // Handle the exception or rethrow.
}
catch (RuntimeException x)
{
   // RuntimeExceptions are always possible.
   // Catch them here to prevent they are
   // ignored by the catch(Exception ignored)
   // block below.
   throw x;
}
catch (Exception ignored)
{
   // No other checked exceptions are thrown
   // by the Task (the compiler will enforce this),
   // so we can safely ignore it, but we're forced
   // to write this catch block: Worker.post(Task t)
   // requires it.
}
The Workerclass, from Foxtrot 2.x, has the following public methods to deal with theEventPumpandWorkerThreadcomponents: 
public static EventPump getEventPump()public static void setEventPump(EventPump pump)public static WorkerThread getWorkerThread()public static void setWorkerThread(WorkerThread worker) Foxtrot configures itself automatically with the most suitable implementation of
EventPumpandWorkerThread.
Some implementations ofEventPumporWorkerThreadallow an even further customization
of the component. For example, implementations of EventPumpthat also implement thefoxtrot.pumps.EventFilterableinterface may allow the user to filter events that are being dispatched
by thejava.awt.EventQueue. See also the bundled Javadocs for further details.However, it is recommended not to exploit these features unless knowing exactly what one is doing:
Foxtrot's defaults may change from version to version to suit better implementations, and these defaults may depend
on the Java Runtime Environment version Foxtrot is running on, so that features working in JDK 1.3.x may not work
in JDK 1.4.x or viceversa.
 Playing with AWT events too badly is normally looking for troubles, so consider you warned :)
 The same holds for WorkerThreadimplementations, that should extend the abstract classAbstractWorkerThread: Foxtrot uses a synchronous model, so replacing (for example)
the defaultWorkerThreadimplementation (that uses a single worker thread) with an implementation
that uses multiple pooled threads does not lead (in the common case) to a great speed-up ofTasks
execution.To be more precise, with a multiple thread implementation of
 WorkerThread,Tasksposted
from pumped events will return control to the initial caller after a time that is (roughly) the time of the
longestTask, while with the default implementation will be after a time that is (roughly) the sum
of the times of allTasks posted from pumped events.
 Tasks posted from pumped events are normally a rare case, and may not be worth the effort of
a multiple thread implementation ofWorkerThread. Refer to the bundled Javadoc documentation for further information, and to the bundled examples for
further details on how to use the Foxtrot classes with Swing. And do not forget the
Tip 'n' Tricks section ! 
 |