You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2003/05/17 20:30:19 UTC

cvs commit: avalon-excalibur/thread/src/java/org/apache/excalibur/thread/impl WorkerThread.java

leif        2003/05/17 11:30:19

  Modified:    thread/src/java/org/apache/avalon/excalibur/thread/impl
                        SimpleWorkerThread.java
               thread/src/java/org/apache/excalibur/thread/impl
                        WorkerThread.java
  Log:
  Fixed a bad thread leak.  When a worker thread was trimmed from a thread pool it
  was entering a wait state rather than terminating, this was leading to the pool
  thinking it was freed.  Over time this would lead to an impressive number of idle
  threads...
  Also added a detail child logger so that problems like this can be more easily
  tracked down by being able to set the main logger to DEBUG, and reduce the
  total output by setting the detail child logger to INFO.
  
  Revision  Changes    Path
  1.8       +55 -2     avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/SimpleWorkerThread.java
  
  Index: SimpleWorkerThread.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/thread/src/java/org/apache/avalon/excalibur/thread/impl/SimpleWorkerThread.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SimpleWorkerThread.java	11 Apr 2003 06:00:29 -0000	1.7
  +++ SimpleWorkerThread.java	17 May 2003 18:30:19 -0000	1.8
  @@ -67,7 +67,15 @@
       extends WorkerThread
       implements Poolable, LogEnabled
   {
  +    /** Log major events like uncaught exceptions and worker creation
  +     *   and deletion.  Stuff that is useful to be able to see over long
  +     *   periods of time. */
       private Logger m_logger;
  +    
  +    /**
  +     * Log minor detail events like 
  +     */
  +    private Logger m_detailLogger;
   
       /**
        * Allocates a new <code>Worker</code> object.
  @@ -82,8 +90,15 @@
       public void enableLogging( final Logger logger )
       {
           m_logger = logger;
  +        m_detailLogger = logger.getChildLogger( "detail" );
       }
   
  +    /**
  +     * Used to log major events against the worker.  Creation, deletion,
  +     *  uncaught exceptions etc.
  +     *
  +     * @param message Message to log.
  +     */
       protected void debug( final String message )
       {
           if ( m_logger.isDebugEnabled() )
  @@ -92,11 +107,49 @@
           }
       }
   
  -    protected void debug( final String message, final Throwable t )
  +    /**
  +     * Used to log major events against the worker.  Creation, deletion,
  +     *  uncaught exceptions etc.
  +     *
  +     * @param message Message to log.
  +     * @param throwable Throwable to log with the message.
  +     */
  +    protected void debug( final String message, final Throwable throwable )
       {
           if ( m_logger.isDebugEnabled() )
           {
  -            m_logger.debug( getName() + ": " + message, t );
  +            m_logger.debug( getName() + ": " + message, throwable );
  +        }
  +    }
  +
  +    /**
  +     * Used to log minor events against the worker.  Start and stop of
  +     *  individual pieces of work etc.  Separated from the major events
  +     *  so that they are not lost in a sea of minor events.
  +     *
  +     * @param message Message to log.
  +     */
  +    protected void detailDebug( final String message )
  +    {
  +        if ( m_detailLogger.isDebugEnabled() )
  +        {
  +            m_detailLogger.debug( getName() + ": " + message );
  +        }
  +    }
  +
  +    /**
  +     * Used to log minor events against the worker.  Start and stop of
  +     *  individual pieces of work etc.  Separated from the major events
  +     *  so that they are not lost in a sea of minor events.
  +     *
  +     * @param message Message to log.
  +     * @param throwable Throwable to log with the message.
  +     */
  +    protected void detailDebug( final String message, final Throwable throwable )
  +    {
  +        if ( m_detailLogger.isDebugEnabled() )
  +        {
  +            m_detailLogger.debug( getName() + ": " + message, throwable );
           }
       }
   }
  
  
  
  1.8       +226 -85   avalon-excalibur/thread/src/java/org/apache/excalibur/thread/impl/WorkerThread.java
  
  Index: WorkerThread.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/thread/src/java/org/apache/excalibur/thread/impl/WorkerThread.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- WorkerThread.java	11 Apr 2003 13:55:45 -0000	1.7
  +++ WorkerThread.java	17 May 2003 18:30:19 -0000	1.8
  @@ -63,6 +63,18 @@
       extends Thread
   {
       /**
  +     * Enables debug output of major events.  Subclasses which implement
  +     *  their own logging do not require this to be true.
  +     */
  +    private static final boolean ENABLE_DEBUG = false;
  +    
  +    /**
  +     * Enables debug output of minor events.  Subclasses which implement
  +     *  their own logging do not require this to be true.
  +     */
  +    private static final boolean ENABLE_DETAIL_DEBUG = false;
  +    
  +    /**
        * The work currentlyy associated with worker (May be null).
        */
       private Executable m_work;
  @@ -120,53 +132,107 @@
       public final synchronized void run()
       {
           debug( "starting." );
  -
  -        // Notify the pool this worker started running.
  -        //notifyAll();
  -
  -        while( m_alive )
  +        try
           {
  -            waitUntilCondition( true );
  -
  -            debug( "running." );
  -
  -            try
  -            {
  -                preExecute();
  -                m_work.execute();
  -                if (m_clearInterruptFlag) clearInterruptFlag();
  -                m_threadControl.finish( null );
  -            }
  -            catch( final ThreadDeath threadDeath )
  -            {
  -                debug( "thread has died." );
  -                m_threadControl.finish( threadDeath );
  -                // This is to let the thread death propagate to the runtime
  -                // enviroment to let it know it must kill this worker
  -                throw threadDeath;
  -            }
  -            catch( final Throwable throwable )
  -            {
  -                // Error thrown while working.
  -                debug( "error caught", throwable );
  -                m_threadControl.finish( throwable );
  -            }
  -            finally
  +            while( m_alive )
               {
  -                debug( "done." );
  -                m_work = null;
  -                m_threadControl = null;
  -                if (m_clearInterruptFlag) clearInterruptFlag();
  -                postExecute();
  +                waitForWork();
  +                if ( m_alive )
  +                {
  +                    detailDebug( "start with work: " + m_work );
  +                    
  +                    try
  +                    {
  +                        try
  +                        {
  +                            preExecute();
  +                            
  +                            // Actually do the work.
  +                            m_work.execute();
  +                            
  +                            // Completed without error, so notify the thread control
  +                            m_threadControl.finish( null );
  +                        }
  +                        catch( final ThreadDeath threadDeath )
  +                        {
  +                            debug( "thread has died." );
  +                            m_threadControl.finish( threadDeath );
  +                            
  +                            // This is to let the thread death propagate to the runtime
  +                            // enviroment to let it know it must kill this worker
  +                            throw threadDeath;
  +                        }
  +                        catch( final Throwable throwable )
  +                        {
  +                            // Error thrown while working.
  +                            debug( "error caught", throwable );
  +                            m_threadControl.finish( throwable );
  +                        }
  +                    }
  +                    finally
  +                    {
  +                        detailDebug( "done with work: " + m_work );
  +                        
  +                        m_work = null;
  +                        m_threadControl = null;
  +                        
  +                        if ( m_clearInterruptFlag )
  +                        {
  +                            clearInterruptFlag();
  +                        }
  +                        
  +                        postExecute();
  +                    }
  +                    
  +                    /*
  +                    try
  +                    {
  +                        preExecute();
  +                        m_work.execute();
  +                        if (m_clearInterruptFlag) clearInterruptFlag();
  +                        m_threadControl.finish( null );
  +                    }
  +                    catch( final ThreadDeath threadDeath )
  +                    {
  +                        debug( "thread has died." );
  +                        m_threadControl.finish( threadDeath );
  +                        // This is to let the thread death propagate to the runtime
  +                        // enviroment to let it know it must kill this worker
  +                        throw threadDeath;
  +                    }
  +                    catch( final Throwable throwable )
  +                    {
  +                        // Error thrown while working.
  +                        debug( "error caught", throwable );
  +                        m_threadControl.finish( throwable );
  +                    }
  +                    finally
  +                    {
  +                        detailDebug( "done." );
  +                        m_work = null;
  +                        m_threadControl = null;
  +                        if (m_clearInterruptFlag) clearInterruptFlag();
  +                        postExecute();
  +                    }
  +                    */
  +        
  +                    //should this be just notify or notifyAll ???
  +                    //It seems to resource intensive option to use notify()
  +                    //notifyAll();
  +                    
  +                    // Let the thread, if any, waiting for the work to complete
  +                    //  know we are done.  Should never me more than one thread
  +                    //  waiting, so notifyAll is not necessary.
  +                    notify();
  +        
  +                    // recycle ourselves
  +                    recycleThread();
  +                }
               }
  -
  -            //should this be just notify or notifyAll ???
  -            //It seems to resource intensive option to use notify()
  -            //notifyAll();
  -            notify();
  -
  -            // recycle ourselves
  -            recycleThread();
  +        }
  +        finally
  +        {
  +            debug( "stopped." );
           }
       }
   
  @@ -175,11 +241,17 @@
        */
       protected void recycleThread()
       {
  -        if( m_alive )
  +        if ( !m_alive )
           {
  -            if (m_clearInterruptFlag) clearInterruptFlag();
  -            m_pool.releaseWorker( this );
  +            throw new IllegalStateException( "Attempted to recycle dead thread." );
           }
  +        
  +        detailDebug( "recycle." );
  +        if ( m_clearInterruptFlag )
  +        {
  +            clearInterruptFlag();
  +        }
  +        m_pool.releaseWorker( this );
       }
   
       /**
  @@ -202,15 +274,15 @@
       }
   
       /**
  -	 * Clears the interrupt flag for this thread.  Since Java does
  +     * Clears the interrupt flag for this thread.  Since Java does
        * not provide a method that does this for an external thread,
        * we have to verify that we are in the WorkerThread.  If the
        * code calling this method does not originate from this thread,
        * we set a flag and wait for it to be called internally.
  -	 */
  -	public void clearInterruptFlag()
  +     */
  +    public void clearInterruptFlag()
       {
  -		if (Thread.currentThread().equals(this))
  +        if (Thread.currentThread().equals(this))
           {
               Thread.interrupted();
               m_clearInterruptFlag = false;
  @@ -219,19 +291,29 @@
           {
               m_clearInterruptFlag = true;
           }
  -	}
  +    }
   
  -	/**
  +    /**
        * Set the <tt>alive</tt> variable to false causing the worker to die.
        * If the worker is stalled and a timeout generated this call, this method
        * does not change the state of the worker (that must be destroyed in other
        * ways).
  +     * <p>
  +     * This is called by the pool when it is removed.
        */
       public void dispose()
       {
           debug( "destroying." );
  +        
           m_alive = false;
  -        waitUntilCondition( false );
  +        
  +        // Notify the thread so it will be woken up and notice that it has been destroyed.
  +        synchronized(this)
  +        {
  +            this.notify();
  +        }
  +        
  +        debug( "destroyed." );
       }
   
       /**
  @@ -243,7 +325,7 @@
           m_work = work;
           m_threadControl = new DefaultThreadControl( this );
   
  -        debug( "notifying this worker." );
  +        detailDebug( "notifying this worker of new work: " + work.toString() );
           notify();
   
           return m_threadControl;
  @@ -254,63 +336,122 @@
        * execute and <i>notifies</i> its thread to do it. Wait
        * until the executable has finished before returning.
        */
  -    protected synchronized void executeAndWait( final Executable work )
  +    protected void executeAndWait( final Executable work )
       {
  +        // Assign work to the thread.
           execute( work );
  -        waitUntilCondition( false );
  +        
  +        // Wait for the work to complete.
  +        synchronized(this)
  +        {
  +            while( null != m_work )
  +            {
  +                try
  +                {
  +                    detailDebug( "waiting for work to complete." );
  +                    wait();
  +                    detailDebug( "notified." );
  +                }
  +                catch( final InterruptedException ie )
  +                {
  +                    // Ignore
  +                }
  +            }
  +        }
       }
  -
  +    
       /**
  -     * Wait until the worker either has work or doesn't have work.
  -     *
  -     * @param hasWork true if waiting till work is present, false otherwise
  +     * For for new work to arrive or for the thread to be destroyed.
        */
  -    private synchronized void waitUntilCondition( final boolean hasWork )
  +    private void waitForWork()
       {
  -        while( hasWork == ( null == m_work ) )
  +        synchronized(this)
           {
  -            try
  -            {
  -                debug( "waiting." );
  -                wait();
  -                debug( "notified." );
  -            }
  -            catch( final InterruptedException ie )
  +            while( m_alive && ( null == m_work ) )
               {
  +                try
  +                {
  +                    detailDebug( "waiting for work." );
  +                    wait();
  +                    detailDebug( "notified." );
  +                }
  +                catch( final InterruptedException ie )
  +                {
  +                    // Ignore
  +                }
               }
           }
       }
   
       /**
  -     * Write a debug message.
  -     * A Noop oin this implementation. Subclasses can overide
  -     * to actually do some logging.
  +     * Used to log major events against the worker.  Creation, deletion,
  +     *  uncaught exceptions etc.
  +     * <p>
  +     * This implementation is a Noop.  Subclasses can override to actually
  +     *  do some logging.
        *
  -     * @param message the message to write out
  +     * @param message Message to log.
        */
       protected void debug( final String message )
       {
  -        if( false )
  +        if( ENABLE_DEBUG )
           {
  -            final String output = getName() + ": " + message;
  -            System.out.println( output );
  +            System.out.println( getName() + ": " + message );
           }
       }
       
       /**
  -     * Write a debug message.
  -     * A Noop oin this implementation. Subclasses can overide
  -     * to actually do some logging.
  +     * Used to log major events against the worker.  Creation, deletion,
  +     *  uncaught exceptions etc.
  +     * <p>
  +     * This implementation is a Noop.  Subclasses can override to actually
  +     *  do some logging.
        *
  -     * @param message the message to write out.
  -     * @param throwable the throwable to write out with the message.
  +     * @param message Message to log.
  +     * @param throwable Throwable to log with the message.
        */
       protected void debug( final String message, final Throwable throwable )
       {
  -        if( false )
  +        if( ENABLE_DEBUG )
  +        {
  +            System.out.println( getName() + ": " + message + ": " + throwable );
  +        }
  +    }
  +    
  +    /**
  +     * Used to log minor events against the worker.  Start and stop of
  +     *  individual pieces of work etc.  Separated from the major events
  +     *  so that they are not lost in a sea of minor events.
  +     * <p>
  +     * This implementation is a Noop.  Subclasses can override to actually
  +     *  do some logging.
  +     *
  +     * @param message Message to log.
  +     */
  +    protected void detailDebug( final String message )
  +    {
  +        if ( ENABLE_DETAIL_DEBUG )
  +        {
  +            System.out.println( getName() + ": " + message );
  +        }
  +    }
  +    
  +    /**
  +     * Used to log minor events against the worker.  Start and stop of
  +     *  individual pieces of work etc.  Separated from the major events
  +     *  so that they are not lost in a sea of minor events.
  +     * <p>
  +     * This implementation is a Noop.  Subclasses can override to actually
  +     *  do some logging.
  +     *
  +     * @param message Message to log.
  +     * @param throwable Throwable to log with the message.
  +     */
  +    protected void detailDebug( final String message, final Throwable throwable )
  +    {
  +        if ( ENABLE_DETAIL_DEBUG )
           {
  -            final String output = getName() + ": " + message + ": " + throwable;
  -            System.out.println( output );
  +            System.out.println( getName() + ": " + message + ": " + throwable );
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org