org.elkoserver.foundation.run
Class Runner

java.lang.Object
  extended by org.elkoserver.foundation.run.Runner
All Implemented Interfaces:
Runnable

public class Runner
extends Object
implements Runnable

Runs when it can, but never on empty. A thread services a queue of Runnables.


Constructor Summary
Runner()
          Makes a Runner, and starts the thread that services its queue.
Runner(String name)
          Makes a Runner, and starts the thread that services its queue.
 
Method Summary
static Runner currentRunner()
          If called from within a thread servicing a Runner, returns that Runner.
 void enqueue(Runnable todo)
          Queues something for this Runnable's thread to do.
 boolean isCurrentThreadInRunner()
          Tests whether the current thread is holding the run lock
 boolean isShuttingDown()
          Tests if an orderlyShutdown been requested.
 Object now(Callable<Object> todo)
          Schedules a thunk to execute "inside" this runner (in the RunnerThread as a separate turn while holding the runLock), while also effectively executing as a synchronous call within the requestors's thread.
 void orderlyShutdown()
          Will enqueue a request to shut down this runner's thread.
 void run()
          Called only by Thread.start().
static void throwIfMandatory(Throwable t)
          Utility routine to either swallow or throw exceptions, depending on whether or not they are the kind of exceptions that need to escape from the run loop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Runner

public Runner()
Makes a Runner, and starts the thread that services its queue. The name of the thread will be "Elko RunQueue".


Runner

public Runner(String name)
Makes a Runner, and starts the thread that services its queue.

Parameters:
name - is the name to give to the thread created.
Method Detail

currentRunner

public static Runner currentRunner()
If called from within a thread servicing a Runner, returns that Runner. Otherwise, returns the default Runner.


throwIfMandatory

public static void throwIfMandatory(Throwable t)
Utility routine to either swallow or throw exceptions, depending on whether or not they are the kind of exceptions that need to escape from the run loop.


enqueue

public void enqueue(Runnable todo)
Queues something for this Runnable's thread to do. May be called from any thead.


isCurrentThreadInRunner

public boolean isCurrentThreadInRunner()
Tests whether the current thread is holding the run lock


now

public Object now(Callable<Object> todo)
Schedules a thunk to execute "inside" this runner (in the RunnerThread as a separate turn while holding the runLock), while also effectively executing as a synchronous call within the requestors's thread. In most ways this can be thought of as a symmetric rendezvous between the two Threads. The reason we *specify* that the thunk is executed specifically in the requested runner's RunnerThread is so that thread-scoped state, such as Runner.currentRunner(), will be according to the Runner receiving the now() request, not whatever thread made the request.


orderlyShutdown

public void orderlyShutdown()
Will enqueue a request to shut down this runner's thread. Since this is an enqueued request, the thread will only shut down after finishing earlier requests, as well as any now()s that happen in the meantime.


isShuttingDown

public boolean isShuttingDown()
Tests if an orderlyShutdown been requested. Note that messages already enqueued will still be serviced before the shutdown request is honored.

Returns:
true if the Runner is shutting down

run

public void run()
Called only by Thread.start(). Pulls Runnables off of the queue until there aren't any more, then waits until there's more to do.

Specified by:
run in interface Runnable