You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/09/28 11:31:01 UTC

cvs commit: jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl BasicThreadPool.java DefaultThreadPool.java ResourceLimitingThreadPool.java DefaultThreadControl.java ExecutableRunnable.java SimpleThreadPool.java WorkerThread.java

donaldp     2002/09/28 02:31:01

  Modified:    thread   default.properties
               thread/src/java/org/apache/avalon/excalibur/thread
                        ThreadPool.java
               thread/src/java/org/apache/avalon/excalibur/thread/impl
                        DefaultThreadPool.java
                        ResourceLimitingThreadPool.java
  Added:       thread/src/java/org/apache/avalon/excalibur/thread/impl
                        BasicThreadPool.java
  Removed:     thread/src/java/org/apache/avalon/excalibur/thread/impl
                        DefaultThreadControl.java ExecutableRunnable.java
                        SimpleThreadPool.java WorkerThread.java
  Log:
  Updated the thread implementation to support the new Avalon/Pool independent API. Maintain backwards compatability using a few hoops.
  
  Deprecated all code except the Default and ResourceLimiting ThreadPools which have not yet been reimplemented.
  
  Revision  Changes    Path
  1.13      +2 -2      jakarta-avalon-excalibur/thread/default.properties
  
  Index: default.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/thread/default.properties,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- default.properties	7 Aug 2002 16:33:23 -0000	1.12
  +++ default.properties	28 Sep 2002 09:31:01 -0000	1.13
  @@ -33,8 +33,8 @@
   excalibur-pool.jar=${excalibur-pool.lib}/excalibur-pool-1.1.jar
   
   # ----- Excalibur instrument, version 0.1 or later -----
  -excalibur-instrument.home=${basedir}/../instrument/dist
  -excalibur-instrument.lib=${excalibur-instrument.home}
  +excalibur-instrument.home=${basedir}/../instrument/build
  +excalibur-instrument.lib=${excalibur-instrument.home}/lib
   excalibur-instrument.jar=${excalibur-instrument.lib}/excalibur-instrument-0.3.jar
   
   # ----- Excalibur Collections, version 1.0 or later -----
  
  
  
  1.3       +3 -9      jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/ThreadPool.java
  
  Index: ThreadPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/ThreadPool.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ThreadPool.java	5 Aug 2002 14:21:06 -0000	1.2
  +++ ThreadPool.java	28 Sep 2002 09:31:01 -0000	1.3
  @@ -8,23 +8,17 @@
   package org.apache.avalon.excalibur.thread;
   
   import org.apache.avalon.framework.activity.Executable;
  +import org.apache.excalibur.thread.ThreadControl;
   
   /**
    * This class is the public frontend for the thread pool code.
    *
    * @author <a href="mailto:peter at apache.org">Peter Donald</a>
  + * @deprecated Replaced with org.apache.excalibur.thread.ThreadPool
    */
   public interface ThreadPool
  +    extends org.apache.excalibur.thread.ThreadPool
   {
  -    /**
  -     * Run work in separate thread.
  -     * Return a valid ThreadControl to control work thread.
  -     *
  -     * @param work the work to be executed.
  -     * @return the ThreadControl
  -     */
  -    ThreadControl execute( Runnable work );
  -
       /**
        * Run work in separate thread.
        * Return a valid ThreadControl to control work thread.
  
  
  
  1.10      +20 -48    jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/DefaultThreadPool.java
  
  Index: DefaultThreadPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/DefaultThreadPool.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DefaultThreadPool.java	5 Aug 2002 14:21:06 -0000	1.9
  +++ DefaultThreadPool.java	28 Sep 2002 09:31:01 -0000	1.10
  @@ -9,7 +9,6 @@
   
   import org.apache.avalon.excalibur.pool.ObjectFactory;
   import org.apache.avalon.excalibur.pool.SoftResourceLimitingPool;
  -import org.apache.avalon.excalibur.thread.ThreadControl;
   import org.apache.avalon.excalibur.thread.ThreadPool;
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.activity.Executable;
  @@ -17,6 +16,8 @@
   import org.apache.avalon.framework.logger.LogKitLogger;
   import org.apache.avalon.framework.logger.Loggable;
   import org.apache.avalon.framework.logger.Logger;
  +import org.apache.avalon.framework.container.ContainerUtil;
  +import org.apache.excalibur.thread.ThreadControl;
   import org.apache.excalibur.threadcontext.ThreadContext;
   
   /**
  @@ -29,13 +30,8 @@
       extends ThreadGroup
       implements ObjectFactory, Loggable, LogEnabled, Disposable, ThreadPool
   {
  -    private SoftResourceLimitingPool m_pool;
  -
  -    private int m_level;
  -
  -    private Logger m_logger;
  -
  -    private ThreadContext m_context;
  +    private final BasicThreadPool m_pool;
  +    private SoftResourceLimitingPool m_underlyingPool;
   
       public DefaultThreadPool( final int capacity )
           throws Exception
  @@ -55,8 +51,8 @@
           throws Exception
       {
           super( name );
  -        m_pool = new SoftResourceLimitingPool( this, capacity );
  -        m_context = context;
  +        m_underlyingPool = new SoftResourceLimitingPool( this, capacity );
  +        m_pool = new BasicThreadPool( this, name, m_underlyingPool, context );
       }
   
       public void setLogger( final org.apache.log.Logger logger )
  @@ -66,45 +62,27 @@
   
       public void enableLogging( final Logger logger )
       {
  -        m_logger = logger;
  -        m_pool.enableLogging( m_logger );
  +        ContainerUtil.enableLogging( m_pool, logger );
       }
   
       public void dispose()
       {
           m_pool.dispose();
  -        m_pool = null;
       }
   
       public Object newInstance()
       {
  -        final String name = getName() + " Worker #" + m_level++;
  -
  -        ThreadContext context = null;
  -        if( null != m_context )
  -        {
  -            context = m_context.duplicate();
  -        }
  -
  -        final WorkerThread worker =
  -            new WorkerThread( this, name, m_pool, context );
  -        worker.setDaemon( true );
  -        worker.enableLogging( m_logger );
  -        worker.start();
  -        return worker;
  +        return m_pool.newInstance();
       }
   
       public void decommission( final Object object )
       {
  -        if( object instanceof WorkerThread )
  -        {
  -            ((WorkerThread)object).dispose();
  -        }
  +        m_pool.decommission( object );
       }
   
       public Class getCreatedClass()
       {
  -        return WorkerThread.class;
  +        return m_pool.getCreatedClass();
       }
   
       /**
  @@ -114,9 +92,9 @@
        * @param work the work to be executed.
        * @return the ThreadControl
        */
  -    public ThreadControl execute( final Runnable work )
  +    public ThreadControl execute( final Executable work )
       {
  -        return execute( new ExecutableRunnable( work ) );
  +        return m_pool.execute( work );
       }
   
       /**
  @@ -126,26 +104,20 @@
        * @param work the work to be executed.
        * @return the ThreadControl
        */
  -    public ThreadControl execute( final Executable work )
  +    public ThreadControl execute( final Runnable work )
       {
  -        final WorkerThread worker = getWorker();
  -        return worker.execute( work );
  +        return m_pool.execute( work );
       }
   
       /**
  -     * Retrieve a worker thread from pool.
  +     * Run work in separate thread.
  +     * Return a valid ThreadControl to control work thread.
        *
  -     * @return the worker thread retrieved from pool
  +     * @param work the work to be executed.
  +     * @return the ThreadControl
        */
  -    protected WorkerThread getWorker()
  +    public ThreadControl execute( final org.apache.excalibur.thread.Executable work )
       {
  -        try
  -        {
  -            return (WorkerThread)m_pool.get();
  -        }
  -        catch( final Exception e )
  -        {
  -            throw new IllegalStateException( "Unable to access thread pool due to " + e );
  -        }
  +        return m_pool.execute( work );
       }
   }
  
  
  
  1.7       +78 -140   jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/ResourceLimitingThreadPool.java
  
  Index: ResourceLimitingThreadPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/ResourceLimitingThreadPool.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ResourceLimitingThreadPool.java	5 Aug 2002 14:21:06 -0000	1.6
  +++ ResourceLimitingThreadPool.java	28 Sep 2002 09:31:01 -0000	1.7
  @@ -11,13 +11,14 @@
   import org.apache.excalibur.instrument.Instrumentable;
   import org.apache.avalon.excalibur.pool.ObjectFactory;
   import org.apache.avalon.excalibur.pool.ResourceLimitingPool;
  -import org.apache.avalon.excalibur.thread.ThreadControl;
   import org.apache.avalon.excalibur.thread.ThreadPool;
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.activity.Executable;
   import org.apache.avalon.framework.logger.LogEnabled;
   import org.apache.avalon.framework.logger.Logger;
  +import org.apache.avalon.framework.container.ContainerUtil;
   import org.apache.excalibur.threadcontext.ThreadContext;
  +import org.apache.excalibur.thread.ThreadControl;
   
   /**
    * A Thread Pool which can be configured to have a hard limit on the maximum number of threads
  @@ -38,15 +39,16 @@
       extends ThreadGroup
       implements ObjectFactory, LogEnabled, Disposable, ThreadPool, Instrumentable
   {
  -
  -    private ResourceLimitingPool m_pool;
  -    private int m_level;
  -    private Logger m_logger;
  -    private ThreadContext m_context;
  +    private ResourceLimitingPool m_underlyingPool;
   
       /** Instrumentable Name assigned to this Instrumentable */
       private String m_instrumentableName;
   
  +    /**
  +     * The associated thread pool.
  +     */
  +    private BasicThreadPool m_pool;
  +
       /*---------------------------------------------------------------
        * Constructors
        *-------------------------------------------------------------*/
  @@ -130,116 +132,19 @@
       {
           super( name );
   
  -        m_pool = new ResourceLimitingPool
  -            ( this, max, maxStrict, blocking, blockTimeout, trimInterval );
  -        m_context = context;
  -    }
  -
  -    /*---------------------------------------------------------------
  -     * ObjectFactory Methods
  -     *-------------------------------------------------------------*/
  -
  -    /**
  -     * Creates and returns a new <code>WorkerThread</code>.
  -     *
  -     * @return new worker thread
  -     *
  -     */
  -    public Object newInstance()
  -    {
  -        final String name =
  -            new StringBuffer( getName() ).append( " Worker #" ).append( m_level++ ).toString();
  -        final WorkerThread worker = new WorkerThread( this, name, m_pool, m_context );
  -        worker.setDaemon( true );
  -        worker.enableLogging( m_logger );
  -        worker.start();
  -
  -        return worker;
  -    }
  -
  -    /**
  -     * Returns the class of which this <code>ObjectFactory</code> creates instances.
  -     *
  -     * @return WorkerThread.class
  -     *
  -     */
  -    public Class getCreatedClass()
  -    {
  -        return WorkerThread.class;
  -    }
  -
  -    /**
  -     * Cleans up any resources associated with the specified object and takes it
  -     * out of commission.
  -     *
  -     * @param object the object to be decommissioned
  -     *
  -     */
  -    public void decommission( final Object object )
  -    {
  -        if( object instanceof WorkerThread )
  +        m_underlyingPool =
  +            new ResourceLimitingPool( this, max, maxStrict,
  +                                      blocking, blockTimeout,
  +                                      trimInterval );
  +        try
           {
  -            ( (WorkerThread)object ).dispose();
  +            m_pool = new BasicThreadPool(this, name, m_underlyingPool, context );
  +        }
  +        catch( Exception e )
  +        {
  +            final String message = "Unable to create ThreadPool due to " + e;
  +            throw new IllegalStateException( message );
           }
  -    }
  -
  -    /*---------------------------------------------------------------
  -     * LogEnabled Methods
  -     *-------------------------------------------------------------*/
  -    /**
  -     * Set the logger.
  -     *
  -     * @param logger
  -     *
  -     */
  -    public void enableLogging( final Logger logger )
  -    {
  -        m_logger = logger;
  -
  -        m_pool.enableLogging( m_logger );
  -    }
  -
  -    /*---------------------------------------------------------------
  -     * Disposable Methods
  -     *-------------------------------------------------------------*/
  -    /**
  -     * Clean up resources and references.
  -     *
  -     */
  -    public void dispose()
  -    {
  -        m_pool.dispose();
  -
  -        m_pool = null;
  -    }
  -
  -    /*---------------------------------------------------------------
  -     * ThreadPool Methods
  -     *-------------------------------------------------------------*/
  -    /**
  -     * Run work in separate thread.
  -     * Return a valid ThreadControl to control work thread.
  -     *
  -     * @param work the work to be executed.
  -     * @return the ThreadControl
  -     */
  -    public ThreadControl execute( final Runnable work )
  -    {
  -        return execute( new ExecutableRunnable( work ) );
  -    }
  -
  -    /**
  -     * Run work in separate thread.
  -     * Return a valid ThreadControl to control work thread.
  -     *
  -     * @param work the work to be executed.
  -     * @return the ThreadControl
  -     */
  -    public ThreadControl execute( final Executable work )
  -    {
  -        final WorkerThread worker = getWorker();
  -
  -        return worker.execute( work );
       }
   
       /*---------------------------------------------------------------
  @@ -300,44 +205,77 @@
        */
       public Instrumentable[] getChildInstrumentables()
       {
  -        return new Instrumentable[]{m_pool};
  +        return new Instrumentable[]{m_underlyingPool};
       }
   
  -    /*---------------------------------------------------------------
  -     * Methods
  -     *-------------------------------------------------------------*/
       /**
  -     * Retrieve a worker thread from pool.
  -     *
  -     * @return the worker thread retrieved from pool
  +     * Return the number of worker threads in the pool.
        *
  -     * @todo remove the line:
  -     * <code>worker.setContextClassLoader(Thread.currentThread().getContextClassLoader());</code>
  +     * @return the numebr of worker threads in the pool.
        */
  -    protected WorkerThread getWorker()
  +    public int getSize()
       {
  -        try
  -        {
  -            final WorkerThread worker = (WorkerThread)m_pool.get();
  +        return m_underlyingPool.getSize();
  +    }
   
  -            // TODO: Remove next line
  -            worker.setContextClassLoader( Thread.currentThread().getContextClassLoader() );
  +    public void enableLogging( final Logger logger )
  +    {
  +        ContainerUtil.enableLogging( m_pool, logger );
  +    }
   
  -            return worker;
  -        }
  -        catch( final Exception e )
  -        {
  -            throw new IllegalStateException( "Unable to access thread pool due to " + e );
  -        }
  +    public void dispose()
  +    {
  +        m_pool.dispose();
  +    }
  +
  +    public Object newInstance()
  +    {
  +        return m_pool.newInstance();
  +    }
  +
  +    public void decommission( final Object object )
  +    {
  +        m_pool.decommission( object );
  +    }
  +
  +    public Class getCreatedClass()
  +    {
  +        return m_pool.getCreatedClass();
       }
   
       /**
  -     * Return the number of worker threads in the pool.
  +     * Run work in separate thread.
  +     * Return a valid ThreadControl to control work thread.
        *
  -     * @return the numebr of worker threads in the pool.
  +     * @param work the work to be executed.
  +     * @return the ThreadControl
        */
  -    public int getSize()
  +    public ThreadControl execute( final Executable work )
  +    {
  +        return m_pool.execute( work );
  +    }
  +
  +    /**
  +     * Run work in separate thread.
  +     * Return a valid ThreadControl to control work thread.
  +     *
  +     * @param work the work to be executed.
  +     * @return the ThreadControl
  +     */
  +    public ThreadControl execute( final Runnable work )
  +    {
  +        return m_pool.execute( work );
  +    }
  +
  +    /**
  +     * Run work in separate thread.
  +     * Return a valid ThreadControl to control work thread.
  +     *
  +     * @param work the work to be executed.
  +     * @return the ThreadControl
  +     */
  +    public ThreadControl execute( final org.apache.excalibur.thread.Executable work )
       {
  -        return m_pool.getSize();
  +        return m_pool.execute( work );
       }
   }
  
  
  
  1.1                  jakarta-avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/BasicThreadPool.java
  
  Index: BasicThreadPool.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.txt file.
   */
  package org.apache.avalon.excalibur.thread.impl;
  
  import org.apache.avalon.excalibur.pool.ObjectFactory;
  import org.apache.avalon.excalibur.pool.SoftResourceLimitingPool;
  import org.apache.avalon.excalibur.pool.Pool;
  import org.apache.avalon.excalibur.thread.ThreadPool;
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Executable;
  import org.apache.avalon.framework.logger.LogEnabled;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.container.ContainerUtil;
  import org.apache.excalibur.thread.ThreadControl;
  import org.apache.excalibur.thread.impl.AbstractThreadPool;
  import org.apache.excalibur.thread.impl.WorkerThread;
  import org.apache.excalibur.threadcontext.ThreadContext;
  
  /**
   * The ThreadPool that binds to Legacy Pooling implementation.
   *
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @deprecated Only used by deprecated components. Will remove in the future
   */
  class BasicThreadPool
      extends AbstractThreadPool
      implements ObjectFactory, LogEnabled, Disposable, ThreadPool
  {
      /**
       * The underlying pool.
       */
      private Pool m_pool;
  
      /**
       * The logger to use for debugging purposes.
       */
      private Logger m_logger;
  
      /**
       * The base ThreadContext that can be duplicated for
       * each thread.
       */
      private ThreadContext m_context;
  
      /**
       * Create a new ThreadPool with specified capacity.
       *
       * @param threadGroup the thread group used in pool
       * @param name the name of pool (used in naming threads)
       * @param pool the underling pool
       * @param context the thread context associated with pool (May be null).
       * @throws Exception if unable to create pool
       */
      public BasicThreadPool( final ThreadGroup threadGroup,
                                       final String name,
                                       final Pool pool,
                                       final ThreadContext context )
          throws Exception
      {
          super( name, threadGroup );
          if( null == pool )
          {
              throw new NullPointerException( "pool" );
          }
  
          m_pool = pool;
          m_context = context;
      }
  
      /**
       * Setup Logging.
       *
       * @param logger the logger
       */
      public void enableLogging( final Logger logger )
      {
          m_logger = logger;
          ContainerUtil.enableLogging( m_pool, logger );
      }
  
      /**
       * Dispose of underlying pool and cleanup resources.
       */
      public void dispose()
      {
          ContainerUtil.dispose( m_pool );
          m_pool = null;
      }
  
      /**
       * Create new Poolable instance.
       *
       * @return the new Poolable instance
       */
      public Object newInstance()
      {
          return createWorker();
      }
  
      /**
       * Overide newWorkerThread to provide a WorkerThread
       * that is Poolable and LogEnabled.
       *
       * @param name the name of WorkerThread
       * @return the created WorkerThread
       */
      protected WorkerThread newWorkerThread( final String name )
      {
          ThreadContext context = null;
          if( null != m_context )
          {
              context = m_context.duplicate();
          }
          final SimpleWorkerThread thread =
              new SimpleWorkerThread( this, getThreadGroup(), name, context );
          ContainerUtil.enableLogging( thread, m_logger );
          return thread;
      }
  
      public void decommission( final Object object )
      {
          if( object instanceof WorkerThread )
          {
              destroyWorker( (WorkerThread) object );
          }
      }
  
      /**
       * Return the class of poolable instance.
       *
       * @return the class of poolable instance.
       */
      public Class getCreatedClass()
      {
          return SimpleWorkerThread.class;
      }
  
      /**
       * Run work in separate thread.
       * Return a valid ThreadControl to control work thread.
       *
       * @param work the work to be executed.
       * @return the ThreadControl
       */
      public ThreadControl execute( final Executable work )
      {
          return execute( new ExecutableExecuteable( work ) );
      }
  
      /**
       * Retrieve a worker thread from pool.
       *
       * @return the worker thread retrieved from pool
       */
      protected WorkerThread getWorker()
      {
          try
          {
              return (WorkerThread) m_pool.get();
          }
          catch( final Exception e )
          {
              final String message =
                  "Unable to access thread pool due to " + e;
              throw new IllegalStateException( message );
          }
      }
  
      /**
       * Release worker back into pool.
       *
       * @param worker the worker (Should be a {@link SimpleWorkerThread}).
       */
      protected void releaseWorker( final WorkerThread worker )
      {
          m_pool.put( (SimpleWorkerThread) worker );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>