public class Worker extends Object
Tasks and Jobs.
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. ConcurrentWorker| Modifier and Type | Method and Description |
|---|---|
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. |
public static WorkerThread getWorkerThread()
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.public static void setWorkerThread(WorkerThread workerThread)
Tasks subclasses in a thread
that is not the Event Dispatch Thread.IllegalArgumentException - If workerThread is nullgetWorkerThread()public static EventPump getEventPump()
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.setEventPump(foxtrot.EventPump)public static void setEventPump(EventPump eventPump)
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.IllegalArgumentException - If eventPump is nullgetEventPump()public static Object post(Task task) throws Exception
IllegalStateException - if is not called from the Event Dispatch Thread nor
from a worker thread.Exceptionpost(Job job)public static Object post(Job job)
post(Task task), but it does not throw checked exceptions.IllegalStateException - if is not called from the Event Dispatch Thread nor from another Job or Task.post(Task)Copyright © 2002-2011. All Rights Reserved.