From: "Carlos Quiroz" <[EMAIL PROTECTED]>

> Well, this is the risk proposing nice stuff ;-)
> Actually I'm quite interested on developing one. I'll study the current
> examples and make a proposal ASAP. In general the idea will be to develop
> one out of the ObjectPool, creating some amount of initial threads and
> accepting tasks to be execute.

Agreed. The FIFOQueue from the threading project might be useful too.

> This requires the addition of some extra
> methods to execute a thread, for instance:
>
> dispatchTask(Runnable runnable)
>
> Should it be there also the capability to name the threads and give them
> priority, like;
>
> dispatchTask(Runnable runnable, String name)
>
> dispatchTask(Runnable runnable, String name, int Priority)

I'd rather have different ThreadPool instances for different 'names'.

    importantThreadPool.dispatch( aRunnable );
    backgroundThreadPool.dispatch( anotherRunnable );

If need be some seperate Multiplexer object could dispatch or route Runnable
objects to different ThreadPools.

> What about error handling? Should be required that the tasks never throw
an
> exception, or the Pool should take care of catching them, at least I
prefer
> that every task be responsible of their own errors, but with a final
> (silent) exception catch to maintain the thread alive

Yes, Runnable seems the best interface for this purpose, then the Runnable
implementation can take care of its own exceptions however it wishes.

Some auxillary interfaces/helper classes may help for writing tasks which
don't know (or don't want to know) how to handle their own exceptions such
that an ExceptionHandler can be configured with the Task. Something like...

/** represents some task which may throw an exception
  */
public interface Task {
    public void doTask() throws Exception;
}

/** A handler of exceptions such as a logger
  */
public interface ExceptionHandler {
    public void handle(Exception e):
}

/** A Runnable which executes Tasks and may
  * use an ExceptionHandler for handling exceptions
  */
public class RunnableTask implements Runnable {
    private Task task;
    private ExceptionHandler exceptionHandler;

    public RunnableTask(Task task) {
        this.task = task;
    }

    public RunnableTask(Task task, ExceptionHandler exceptionHandler) {
        this.task = task;
        this.exceptionHandler = exceptionHandler;
    }

    public void run() {
        try {
            task.doTask();
        }
        catch (Exception exception) {
            if ( exceptionHandler != null ) {
                exceptionHandler.handle( e );
            }
            else {
                // a logging facade would be
                // nice here ;-)
            }
        }
    }
}


James


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

Reply via email to