public abstract class EventThread extends Thread
run() method that
iterates over a Java BlockingQueue, processing
each event in the same order as posted. The event thread has
three general states:
starting() method
is called to perform the actual initialization. If this
method returns true, the thread moves to the
next state. If false is returned or this method
throws an exception, the event thread stops running.
handleEvent(Object)
method. This continues until the thread is halted.
If haltNow(boolean)
is called, all enqueued events are discarded and the
event thread immediately goes to the next state. If
halt(boolean) is
called, a special halt event is enqueued. The event
thread continues processing events until the halt event
is reached. The event thread then goes to the next
state. In either case, after the event thread is halted,
no more events may be enqueued.
stopping() method.
The subclass performs the necessary clean up before
the event thread stops running.
The event thread supports only a First In, First Out (FIFO) blocking queue with configurable capacity. There is no support for a priority blocking queue.
| Modifier and Type | Class and Description |
|---|---|
static class |
EventThread.RunState
An
EventThread has seven distinct states:
NOT_STARTED: The EventThread was
instantiated but not yet started. |
Thread.State, Thread.UncaughtExceptionHandlerMAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY| Modifier | Constructor and Description |
|---|---|
protected |
EventThread()
Constructs an
EventThread with a capacity of
Integer.MAX_VALUE. |
protected |
EventThread(int capacity)
Constructs a
EventThread with a specified
event queue capacity. |
protected |
EventThread(String name)
Constructs an
EventThread with a specified
thread name and a default capacity of
Integer.MAX_VALUE. |
protected |
EventThread(String name,
int capacity)
Constructs an
EventThread with a specified
name and queue capacity. |
| Modifier and Type | Method and Description |
|---|---|
void |
add(Object event)
Appends an event to the queue.
|
void |
halt(boolean waitFlag)
Permanently stops this thread's execution after
the event queue is empty.
|
void |
haltNow(boolean waitFlag)
Permanently stops this thread's execution now.
|
abstract void |
handleEvent(Object event)
Processes the next enqueued event.
|
boolean |
isHalted()
Returns
true if this event thread has been
halted and false otherwise. |
void |
run()
The main
Thread routine. |
EventThread.RunState |
runstate()
Returns this event thread current
state. |
void |
start(boolean waitFlag)
Starts the thread execution.
|
abstract boolean |
starting()
Returns
true if the EventThread
subclass initializes successfully and false
otherwise. |
abstract void |
stopping()
Performs any necessary local clean up when the thread
halts.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yieldprotected EventThread()
EventThread with a capacity of
Integer.MAX_VALUE. The thread name is set to
"EventThread_n" where n is an arbitrary
number.protected EventThread(String name)
EventThread with a specified
thread name and a default capacity of
Integer.MAX_VALUE.name - Thread name.protected EventThread(int capacity)
EventThread with a specified
event queue capacity. The thread name is set to
"EventThread_n" where n is an arbitrary
number.capacity - Event queue capacity.IllegalArgumentException - if capacity is <= zero.protected EventThread(String name, int capacity)
EventThread with a specified
name and queue capacity.name - Thread name.capacity - Event queue capacity.IllegalArgumentException - if capacity is <= zero.public final boolean isHalted()
true if this event thread has been
halted and false otherwise. It is possible
for this method to return true yet still
receive calls to handleEvent(Object). This
will happen when halt(boolean) is called, the
event queue is not empty and so the thread is still
processing events.
Use runstate() for finer-grained determination
of the event thread's current state.
Call haltNow(boolean) to stop the event thread
immediately despite the presence of unprocessed events.
true if this event thread has been
halted and false otherwise.halt(boolean),
haltNow(boolean),
runstate()public final EventThread.RunState runstate()
state.state.isHalted()public final void run()
Thread routine.
Do not call this method directly. Instead, call
the start method.
public abstract void handleEvent(Object event)
event - the next event.public abstract boolean starting()
true if the EventThread
subclass initializes successfully and false
otherwise. If false is returned or throws an
exception, then this event thread immediately halts.true if the EventThread
subclass initializes successfully and false
otherwise.public abstract void stopping()
public final void start(boolean waitFlag)
throws IllegalStateException
waitFlag is
true, then waits for this thread to begin
execution before return; otherwise returns immediately.
Note: waiting for this event thread to start
does not imply this thread is running, only that its
starting() method has completed. If
starting() returns false, then this
thread will immediately terminate. Call runstate()
to determine if this thread is
running.
waitFlag - if true, then wait for the event
thread to start before returning; otherwise return
immediately.IllegalStateException - if this thread was previously started.public final void add(Object event) throws IllegalArgumentException, IllegalStateException
event - append this event to the queue.IllegalArgumentException - if event is null.IllegalStateException - if this event thread is halted.public final void halt(boolean waitFlag)
add(Object) and
java.lang.IllegalStateException.
If waitFlag is true, then this method
blocks until this event thread has finished processing
all extant events and is halted.
Does nothing if this thread was previously halted and returns immediately.
waitFlag - if true, then wait for the event
thread to halt before returning; otherwise return
immediately.public final void haltNow(boolean waitFlag)
add(Object) and
java.lang.IllegalStateException.
If waitFlag is true, then this method
blocks until this event thread is halted.
Does nothing if this thread was previously halted and returns immediately.
waitFlag - if true, then wait for the event
thread to halt before returning; otherwise return
immediately.Copyright © 2020. All rights reserved.