donaldp 01/03/14 19:42:21 Modified: src/java/org/apache/avalon/util/thread ThreadPool.java WorkerThread.java Added: src/java/org/apache/avalon/util/thread DefaultThreadPool.java Log: Separated out implementation and interface to thread pool. Revision Changes Path 1.4 +40 -89 jakarta-avalon/src/java/org/apache/avalon/util/thread/ThreadPool.java Index: ThreadPool.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/util/thread/ThreadPool.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ThreadPool.java 2001/03/05 18:56:41 1.3 +++ ThreadPool.java 2001/03/15 03:42:21 1.4 @@ -7,99 +7,50 @@ */ package org.apache.avalon.util.thread; -import org.apache.avalon.Loggable; -import org.apache.avalon.Poolable; -import org.apache.avalon.util.pool.ObjectFactory; -import org.apache.avalon.util.pool.SoftResourceLimitingPool; -import org.apache.log.Logger; - /** * This class is the public frontend for the thread pool code. * - * TODO: Should this be configured with min threads, max threads and min spare threads ? - * - * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> */ -public class ThreadPool - extends ThreadGroup - implements ObjectFactory, Loggable +public interface ThreadPool { - protected final SoftResourceLimitingPool m_pool; - protected int m_level; - protected Logger m_logger; - - public ThreadPool( final int capacity ) - throws Exception - { - this( "Worker Pool", capacity ); - } - - public ThreadPool( final String name, final int capacity ) - throws Exception - { - super( name ); - m_pool = new SoftResourceLimitingPool( this, 0 ); - m_pool.init(); - } - - public void setLogger( final Logger logger ) - { - m_logger = logger; - } - - public Poolable newInstance() - { - final WorkerThread worker = - new WorkerThread( this, m_pool, getName() + " Worker #" + m_level++ ); - worker.setLogger( m_logger ); - worker.start(); - return worker; - } - - public void decommission(Poolable object) { - if (object instanceof WorkerThread) { - ((WorkerThread) object).dispose(); - } - } - - public Class getCreatedClass() - { - return WorkerThread.class; - } - - public void execute( final Runnable work ) - throws Exception - { - execute( work, Thread.NORM_PRIORITY ); - } - - public void execute( final Runnable work, final int priority ) - throws Exception - { - final WorkerThread worker = getWorker( priority ); - worker.execute( work ); - } - - public void executeAndWait( final Runnable work ) - throws Exception - { - executeAndWait( work, Thread.NORM_PRIORITY ); - } - - public void executeAndWait( final Runnable work, final int priority ) - throws Exception - { - final WorkerThread worker = getWorker( priority ); - worker.executeAndWait( work ); - } - - protected WorkerThread getWorker( final int priority ) - throws Exception - { - final WorkerThread worker = (WorkerThread)m_pool.get(); - worker.setContextClassLoader( Thread.currentThread().getContextClassLoader() ); - worker.setPriority( priority ); - return worker; - } + /** + * Run work in separate thread. + * + * @param work the work to be executed. + * @exception Exception if an error occurs + */ + void execute( final Runnable work ) + throws Exception; + + /** + * Run work in separate thread at a particular priority. + * + * @param work the work to be executed. + * @param priority the priority + * @exception Exception if an error occurs + */ + void execute( final Runnable work, final int priority ) + throws Exception; + + /** + * Run work in separate thread. + * Wait till work is complete before returning. + * + * @param work the work to be executed. + * @exception Exception if an error occurs + */ + void executeAndWait( final Runnable work ) + throws Exception; + + /** + * Run work in separate thread at a particular priority. + * Wait till work is complete before returning. + * + * @param work the work to be executed. + * @param priority the priority + * @exception Exception if an error occurs + */ + void executeAndWait( final Runnable work, final int priority ) + throws Exception; } 1.3 +3 -2 jakarta-avalon/src/java/org/apache/avalon/util/thread/WorkerThread.java Index: WorkerThread.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/util/thread/WorkerThread.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- WorkerThread.java 2001/03/05 18:56:41 1.2 +++ WorkerThread.java 2001/03/15 03:42:21 1.3 @@ -34,11 +34,12 @@ /** * Allocates a new <code>Worker</code> object. */ - protected WorkerThread( final ThreadPool threadPool, + protected WorkerThread( final ThreadGroup group, + final ThreadPool threadPool, final SoftResourceLimitingPool pool, final String name ) { - super( threadPool, name ); + super( group, name ); m_threadPool = threadPool; m_pool = pool; 1.1 jakarta-avalon/src/java/org/apache/avalon/util/thread/DefaultThreadPool.java Index: DefaultThreadPool.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.util.thread; import org.apache.avalon.Loggable; import org.apache.avalon.Poolable; import org.apache.avalon.util.pool.ObjectFactory; import org.apache.avalon.util.pool.SoftResourceLimitingPool; import org.apache.log.Logger; /** * This class is the public frontend for the thread pool code. * * TODO: Should this be configured with min threads, max threads and min spare threads ? * * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> */ public class DefaultThreadPool extends ThreadGroup implements ObjectFactory, Loggable, ThreadPool { protected final SoftResourceLimitingPool m_pool; protected int m_level; protected Logger m_logger; public DefaultThreadPool( final int capacity ) throws Exception { this( "Worker Pool", capacity ); } public DefaultThreadPool( final String name, final int capacity ) throws Exception { super( name ); m_pool = new SoftResourceLimitingPool( this, 0 ); m_pool.init(); } public void setLogger( final Logger logger ) { m_logger = logger; } public Poolable newInstance() { final WorkerThread worker = new WorkerThread( this, this, m_pool, getName() + " Worker #" + m_level++ ); worker.setLogger( m_logger ); worker.start(); return worker; } public void decommission(Poolable object) { if( object instanceof WorkerThread ) { ((WorkerThread)object).dispose(); } } public Class getCreatedClass() { return WorkerThread.class; } public void execute( final Runnable work ) throws Exception { execute( work, Thread.NORM_PRIORITY ); } public void execute( final Runnable work, final int priority ) throws Exception { final WorkerThread worker = getWorker( priority ); worker.execute( work ); } public void executeAndWait( final Runnable work ) throws Exception { executeAndWait( work, Thread.NORM_PRIORITY ); } public void executeAndWait( final Runnable work, final int priority ) throws Exception { final WorkerThread worker = getWorker( priority ); worker.executeAndWait( work ); } protected WorkerThread getWorker( final int priority ) throws Exception { final WorkerThread worker = (WorkerThread)m_pool.get(); worker.setContextClassLoader( Thread.currentThread().getContextClassLoader() ); worker.setPriority( priority ); return worker; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]