You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by do...@apache.org on 2001/03/15 04:55:36 UTC
cvs commit: jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/thread DefaultThreadPool.java ThreadPool.java WorkerThread.java
donaldp 01/03/14 19:55:36
Modified: proposal/4.0/src/java/org/apache/avalon/thread
ThreadPool.java WorkerThread.java
Added: proposal/4.0/src/java/org/apache/avalon/thread
DefaultThreadPool.java
Log:
Separated interface from implementation.
Revision Changes Path
1.4 +40 -84 jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/thread/ThreadPool.java
Index: ThreadPool.java
===================================================================
RCS file: /home/cvs/jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/thread/ThreadPool.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ThreadPool.java 2001/03/15 03:10:18 1.3
+++ ThreadPool.java 2001/03/15 03:55:36 1.4
@@ -7,94 +7,50 @@
*/
package org.apache.avalon.thread;
-import org.apache.avalon.logger.Loggable;
-import org.apache.avalon.pool.ObjectFactory;
-import org.apache.avalon.pool.Poolable;
-import org.apache.avalon.pool.ThreadSafePool;
-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:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
-public class ThreadPool
- extends ThreadGroup
- implements ObjectFactory, Loggable
+public interface ThreadPool
{
- protected final ThreadSafePool 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 ThreadSafePool( this, 0 );
- m_pool.init();
- m_pool.grow( capacity );
- }
-
- 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 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.4 +22 -21 jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/thread/WorkerThread.java
Index: WorkerThread.java
===================================================================
RCS file: /home/cvs/jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/thread/WorkerThread.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WorkerThread.java 2001/03/15 03:10:19 1.3
+++ WorkerThread.java 2001/03/15 03:55:36 1.4
@@ -18,28 +18,29 @@
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
-class WorkerThread
- extends Thread
+class WorkerThread
+ extends Thread
implements Poolable, Loggable
{
protected final static boolean DEBUG = false;
- protected Logger m_logger;
- protected ThreadPool m_threadPool;
- protected ThreadSafePool m_pool;
+ protected Logger m_logger;
+ protected ThreadPool m_threadPool;
+ protected ThreadSafePool m_pool;
- protected Runnable m_work;
- protected boolean m_alive;
+ protected Runnable m_work;
+ protected boolean m_alive;
/**
* Allocates a new <code>Worker</code> object.
*/
- protected WorkerThread( final ThreadPool threadPool,
- final ThreadSafePool pool,
+ protected WorkerThread( final ThreadGroup group,
+ final ThreadPool threadPool,
+ final ThreadSafePool pool,
final String name )
{
- super( threadPool, name );
-
+ super( group, name );
+
m_threadPool = threadPool;
m_pool = pool;
@@ -61,7 +62,7 @@
ThreadContext.setCurrentThreadPool( m_threadPool );
if( DEBUG ) m_logger.info( getName() + ": starting." );
-
+
// Notify the pool this worker started running.
//notifyAll();
@@ -70,19 +71,19 @@
waitUntilCondition( true );
if( DEBUG ) m_logger.debug( getName() + ": running." );
-
- try
+
+ try
{
m_work.run();
- }
+ }
catch( final ThreadDeath td )
{
if ( DEBUG ) m_logger.debug( getName() + ": thread has died." );
-
+
// This is to let the thread death propagate to the runtime
// enviroment to let it know it must kill this worker
throw td;
- }
+ }
catch( final Throwable t )
{
// Error thrown while working.
@@ -94,7 +95,7 @@
m_work = null;
- //should this be just notify or notifyAll ???
+ //should this be just notify or notifyAll ???
//It seems to resource intensive option to use notify()
//notifyAll();
notify();
@@ -125,10 +126,10 @@
{
while( hasWork == (null == m_work) )
{
- try
+ try
{
if( DEBUG ) m_logger.debug( getName() + ": waiting." );
- wait();
+ wait();
if( DEBUG ) m_logger.debug( getName() + ": notified." );
}
catch( final InterruptedException ie ) {}
@@ -148,7 +149,7 @@
* does not change the state of the worker (that must be destroyed in other
* ways).
*/
- public void dispose()
+ public void dispose()
{
if( DEBUG ) m_logger.debug( getName() + ": destroying." );
m_alive = false;
1.1 jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/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.thread;
import org.apache.avalon.logger.Loggable;
import org.apache.avalon.pool.Poolable;
import org.apache.avalon.pool.ObjectFactory;
import org.apache.avalon.pool.ThreadSafePool;
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:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public class DefaultThreadPool
extends ThreadGroup
implements ObjectFactory, Loggable, ThreadPool
{
protected final ThreadSafePool 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 ThreadSafePool( 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;
}
/**
* Run work in separate thread.
*
* @param work the work to be executed.
* @exception Exception if an error occurs
*/
public void execute( final Runnable work )
throws Exception
{
execute( work, Thread.NORM_PRIORITY );
}
/**
* 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
*/
public void execute( final Runnable work, final int priority )
throws Exception
{
final WorkerThread worker = getWorker( priority );
worker.execute( work );
}
/**
* 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
*/
public void executeAndWait( final Runnable work )
throws Exception
{
executeAndWait( work, Thread.NORM_PRIORITY );
}
/**
* 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
*/
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: avalon-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-dev-help@jakarta.apache.org