|
Quaqua 8.0 2011-10-02 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectch.randelshofer.quaqua.util.EventLoop
public abstract class EventLoop
An EventLoop can process events on a separate worker thread. It consists of two parts: the event collector and the event processor.
The event collector collects all incoming events and puts them
into a queue.
The event processor removes the events from the queue and
processes them.
The key feature of the EventLoop is, that clients don't have to wait until an event has been processed. Clients are free to proceed as soon as the collector has put the event into the queue.
Other important features are that events are processed in the same sequence as they have been collected and that only one thread is used to process the events.
Usage
This is an abstract class. It does all the queue handling, but does no processing. To use it, you have to create a subclass which overrides the methods #collectEvent and #processEvent.
Example
An EventLoop, which outputs Strings on a background thread could look like this:
public class AsyncDisplay extends AbstractEventLoop { public void display(String string) { collectEvent(string); } protected void processEvent(Object event) { System.out.println((String) event); } }
To use the class proceed like this:
AsyncDisplay a = new AsyncDisplay(); a.display("Hello World");
Constructor Summary | |
---|---|
EventLoop()
Creates a new EventLoop which processes events at Thread.NORM_PRORITY. |
|
EventLoop(int priority)
Creates a new EventLoop which processes events at the desired thread priority. |
Method Summary | |
---|---|
void |
clear()
Clears the event queue. |
protected void |
collectEvent(java.lang.Object event)
Collects an event and puts it into the event queue for later processing. |
void |
dispose()
|
boolean |
isCoalesce()
Returns true if the EventLoop coalesces multiple pending events. |
boolean |
isLIFO()
Returns true if the EventLoop coalesces multiple pending events. |
protected abstract void |
processEvent(java.lang.Object event)
This method processes an event on the event processor thread. |
protected void |
processEvents()
This method removes events from the event queue and proceses them until the queue is empty or until #stop is called. |
void |
setCoalesce(boolean b)
Sets whether the EventLoop coalesces multiple pending events. |
void |
setLIFO(boolean b)
Sets whether the EventLoop shall enqueue events at the beginning of the queue instead of at the end of the queue. |
void |
start()
Starts the event processor. |
void |
stop()
Stops the event processor. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EventLoop()
public EventLoop(int priority)
priority
- The Thread priority of the event processor.Method Detail |
---|
protected void collectEvent(java.lang.Object event)
event
- The event to be put into the queue.public void setCoalesce(boolean b)
(o==null ? e==null : o.equals(e))
.
EventLoops do not coalesce events by default.
b
- Specify true to turn on coalescing.public boolean isCoalesce()
setCoalesce(boolean)
public void setLIFO(boolean b)
b
- Specify true to enqueue events at the beginning of the queue.public boolean isLIFO()
setCoalesce(boolean)
public void start()
public void stop()
public void clear()
public void dispose()
protected abstract void processEvent(java.lang.Object event)
event
- An event from the queue.protected void processEvents()
This method must be called from the event processor thread only.
|
Copyright 2003-2007 (c) Werner Randelshofer. All rights reserved. |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |