Overview | Package | Class | Tree | Deprecated | Index | Help
| Copyright 2012-04-26 Werner Randelshofer
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.monte.media.concurrent
Class EventLoop

    java.lang.Object
      org.monte.media.concurrent.EventLoop

Direct Known Subclasses:
  SequentialDispatcher

    public abstract class EventLoop

extends java.lang.Object

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.

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");

Version:
  1.5 2011-02-17 Merged with CubeTwister.
  1.4 2008-01-02 Added method join.
  1.3 2002-05-15 Reworked.
  1.2 2001-09-24 Support for coalescing of events added.
  1.1 2001-08-24 Reworked for JDK 1.3.
  1.0.2 2000-03-03 Catch SecurityException's (To make this class work in Netscape Navigator).
  1.0.1 1998-10-14 Adds version info to class comments.
  1.0 1998-10-01 Threads are not reused anymore.
Author:
  Werner Randelshofer

Field Summary
`protected java.lang.Thread` | `eventProcessor` The event processor thread.

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.
`boolean` | `isCoalesce()` Returns true if the EventLoop coalesces multiple pending events.
`void` | `join()`
`protected abstract void` | `processEvent(java.lang.Object event)` This method processes a single 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` | `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`

Field Detail

### eventProcessor

    protected java.lang.Thread eventProcessor

  The event processor thread.

Constructor Detail

### EventLoop

    public EventLoop()

  Creates a new EventLoop which processes events at Thread.NORM_PRORITY.

### EventLoop

    public EventLoop(int priority)

  Creates a new EventLoop which processes events at the desired thread priority.

  Parameters:
    `priority` - The Thread priority of the event processor.

Method Detail

### collectEvent

    protected void collectEvent(java.lang.Object event)

  Collects an event and puts it into the event queue for later processing.

  Parameters:
    `event` - The event to be put into the queue.

### setCoalesce

    public void setCoalesce(boolean b)

  Sets whether the EventLoop coalesces multiple pending events. A busy application may not be able to keep up with event generation, causing multiple events to be queued. Coalescing is based on equality tests of event objects. More formally, coalesces an event o if and only if the queue contains at least one element e such that `(o==null ? e==null : o.equals(e))`.

  EventLoops do not coalesce events by default.

  Parameters:
    `b` - Specify true to turn on coalescing.

### isCoalesce

    public boolean isCoalesce()

  Returns true if the EventLoop coalesces multiple pending events.

  See Also:
    `setCoalesce(boolean)`

### start

    public void start()

  Starts the event processor.
  The event processor is started by default.

### stop

    public void stop()

  Stops the event processor.

### join

    public void join()
              throws java.lang.InterruptedException

  Throws:
    `java.lang.InterruptedException`

### clear

    public void clear()

  Clears the event queue.

### processEvent

    protected abstract void processEvent(java.lang.Object event)

  This method processes a single event on the event processor thread.

  Parameters:
    `event` - An event from the queue.

### processEvents

    protected void processEvents()

  This method removes events from the event queue and proceses them until the queue is empty or until #stop is called.

  This method must be called from the event processor thread only.

Overview | Package | Class | Tree | Deprecated | Index | Help
| Copyright 2012-04-26 Werner Randelshofer
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD