Main Page Namespace List Class Hierarchy Compound List Compound Members

# ZThread::AsyncExecutor Class Template Reference

`#include <AsyncExecutor.h>`

Inheritance diagram for ZThread::AsyncExecutor::

List of all members.

## Public Methods
| AsyncExecutor () throw (Synchronization_Exception)
virtual | ~AsyncExecutor () throw ()
virtual void | execute (Runnable *task) throw (Synchronization_Exception)
virtual void | join () throw (Synchronization_Exception)
virtual unsigned int | threadCount () throw (Synchronization_Exception)
## Protected Methods
virtual Thread* | createWorker ()
## Protected Attributes
Thread* | _worker
| Worker thread.

## Detailed Description

### template<bool DAEMON> class ZThread::AsyncExecutor

Date:
  03-05-2001

Author:
  Eric Crahen

Version:
  1.2.5

This interface defines the basic outline needed to implement a LWP (light-weigh-process) execution mechanism.

An executor allows the efficient execution of a number of small unrelated 'Runnable' tasks, or LWP's without the over head of a large number of synchronization controls needed in a thread per task solution.

The executor itself contains heavy weight synchronization controls to do this, but it will not need new ones for each additional task. This overcomes resource problems thread-per-task designs must face

In general, executors queue tasks for execution by another thread or by a pool of threads. The queuing of task allows for a good deal of control over the order of execution.

STATIC/NON-STATIC EXECUTORS:

All static executors should be marked as Daemon's using the template parameter. If the executor is non-static then it should not be marked as a daemon.

Static executors will use the new daemon thread feature, and so static executors do not need to be join()ed - however, they still need to be cancel()ed. This allows static executors to complete thier tasks after main exits, and solves static deadlock & deconstruction problems.

     // Proper use of a static AsyncExecutor

     static Executor *executor = new AsyncExecutor<true>;

     int main() {

       // Add tasks

       executor->cancel();

     }

Static executors can not be cancled from within thier destructors. The system will not execute the destructors for static objects until the threads have all been joined. Worker threads will not exit until the executor has been cancled. This will lead to a deadlock situation.

     // Proper use of a non-static AsyncExecutor

     int main() {

       Executor *executor = new AsyncExecutor<false>;

       // Add tasks

       executor->cancel();
       executor->join();
     }

A template parameter was chosen because it allows only the code needed to implement that type of executor to be used, and eliminates the need for a daemon flag within the executor and a series of constant checks to take the approriate action.

The template also better reflects the nature of the choice of executor type. A static executor will not change and suddenly not act static, and vice-versa.

## Constructor & Destructor Documentation

AsyncExecutor ( | ) throw (Synchronization_Exception)

  | Create a new AsyncExecutor, the actual initialization of any underlying resources used in constructing this Executor is deferred until the first task is executed. Parameters:
  bool | - optionally daemon flag the thread, for static executors

~AsyncExecutor ( | ) throw ()`[virtual]`

| Destroy the AsyncExecutor. Precondition: The Executor should be joined prior to its destruction.

## Member Function Documentation

Thread * createWorker ( | )`[inline, protected, virtual]`

| Default worker implementation will consume tasks Reimplemented from ZThread::AbstractExecutor.

void execute ( | Runnable * task ) throw (Synchronization_Exception)`[inline, virtual]`

  | Enqueue another task for execution. Parameters:
  Runnable | task - task to execute
  Exceptions:
  InterruptedException |
  Precondition: The executor must not be canceled. A canceled executor will ignore execute() requests. Postcondition: The given task will be executed as soon as possible by the executor Parameters:
  task |
Reimplemented from ZThread::Executor.

void join ( | ) throw (Synchronization_Exception)`[virtual]`

  | Wait for this Executor to complete. The calling thread is blocked until the last task has been completed by this executor or until the calling thread has been interrupted. Exceptions:
  InterruptedException |
Reimplemented from ZThread::Executor.

unsigned int threadCount ( | ) throw (Synchronization_Exception)`[inline, virtual]`

  | Count of active threads operation on behalf of this executor This does not guarantee that another thread won't have submitted a task that changes the number of executing threads before some action is taken in response to this. However, it is useful as a general heuristic Returns: unsigned int Exceptions:
  InterruptedException |
Reimplemented from ZThread::Executor.

The documentation for this class was generated from the following file:

- AsyncExecutor.h