paratask.runtime
Class EventLoop

java.lang.Object
  extended by paratask.runtime.EventLoop

public class EventLoop
extends java.lang.Object

Thread-specific event loop. Any user-defined thread that makes use of the ParaTask clauses notify, asyncCatch, or notifyInterim must make use of this EventLoop. This also includes the main thread (the thread that invokes the application's main method). This requirement is necessary because such threads do not have an event loop of their own, yet the above ParaTask clauses involve event-based callbacks.

ParaTask threads (i.e. any code executed inside a ParaTask task), and Java's Event Dispatch Thread (EDT) (i.e. any code executed inside handlers such actionPerformed, etc.) must not use this EventLoop since they already have an event loop of their own. However, in order to initialise these event loops, the programmer is required to call ParaTask.init() at the start of the main method of their application.

Note that if any thread has nothing to compute, then that thread will terminate. In particular, this means that if the main thread (the thread that executes the main method) returns from the main method, then essentially the application ends: any other running threads will also be terminated. For example, if the the main thread uses a notifyGUI clause, then the EDT might not have a chance to execute the clause and the also the worker threads themselves might not be able to complete the tasks since the application terminates when the main thread terminates. To avoid this, programmers should invoke ParaTask.init() early in the main method, and then invoke exec() at the end of the main method so that the application remains active.

Author:
Nasser Giacaman, Oliver Sinnen

Method Summary
static int exec()
          The calling thread enters the event loop.
static void quit()
          Stops the event loop for the calling thread.
static boolean register()
          Register a user-defined thread (or the main thread) with ParaTask.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

register

public static boolean register()
Register a user-defined thread (or the main thread) with ParaTask. See class description above for more details. The thread should call this method before it invokes any tasks with ParaTask clauses, and before it enters the event loop.

Returns:
true if this thread is not registered already, otherwise false if already registered.
Throws:
java.lang.UnsupportedOperationException - If attempting to register a ParaTask thread or the EDT
See Also:
exec()

exec

public static int exec()
The calling thread enters the event loop. The thread should be registered before calling this method. The calling thread stays listening to tasks until the quit() method is called.

Returns:
Returns 0 if no error occurs.
Throws:
java.lang.UnsupportedOperationException - If executed by a ParaTask thread or the EDT
See Also:
register(), quit()

quit

public static void quit()
Stops the event loop for the calling thread.

Throws:
java.lang.UnsupportedOperationException - If executed by a ParaTask thread or the EDT
See Also:
exec(), register()