bloritsch 2002/08/09 12:01:12
Modified: event/src/java/org/apache/excalibur/event/command
AbstractThreadManager.java
event/src/java/org/apache/excalibur/thread/impl
DefaultThreadPool.java
event/src/test/org/apache/excalibur/event/command/test
TPCThreadManagerTestCase.java
Added: event/src/java/org/apache/excalibur/mpool
BlockingFixedSizePool.java
Log:
Add an explicit blocking pool
Revision Changes Path
1.6 +21 -14
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/command/AbstractThreadManager.java
Index: AbstractThreadManager.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/command/AbstractThreadManager.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- AbstractThreadManager.java 9 Aug 2002 05:32:03 -0000 1.5
+++ AbstractThreadManager.java 9 Aug 2002 19:01:12 -0000 1.6
@@ -248,24 +248,31 @@
while( i.hasNext() )
{
- try
- {
- m_controls.add(m_threadPool.execute( ( PipelineRunner )
i.next() ));
- }
- catch( IllegalStateException e )
- {
- // that's the way ResourceLimitingThreadPool reports
- // that it has no threads available, will still try
- // to go on, hopefully at one point there will be
- // a thread to execute our runner
+ ThreadControl control = null;
- if( getLogger().isWarnEnabled() )
+ while (control == null )
+ {
+ try
{
- getLogger().warn( "Unable to execute pipeline (If
out of threads, "
- + "increase block-timeout or
number of threads "
- + "per processor", e );
+ control = m_threadPool.execute( ( PipelineRunner )
i.next() );
+ }
+ catch( IllegalStateException e )
+ {
+ // that's the way ResourceLimitingThreadPool reports
+ // that it has no threads available, will still try
+ // to go on, hopefully at one point there will be
+ // a thread to execute our runner
+
+ if( getLogger().isWarnEnabled() )
+ {
+ getLogger().warn( "Unable to execute pipeline
(If out of threads, "
+ + "increase block-timeout or
number of threads "
+ + "per processor", e );
+ }
}
}
+
+ m_controls.add(control);
}
}
finally
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/BlockingFixedSizePool.java
Index: BlockingFixedSizePool.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
must not be used to endorse or promote products derived from this software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
import org.apache.avalon.excalibur.concurrent.Mutex;
import org.apache.avalon.excalibur.collections.Buffer;
import org.apache.avalon.excalibur.collections.FixedSizeBuffer;
import org.apache.avalon.framework.activity.Disposable;
/**
* This is an <code>Pool</code> that caches Poolable objects for reuse.
* Please note that this pool offers no resource limiting whatsoever.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/09 19:01:12 $
* @since 4.1
*/
public final class BlockingFixedSizePool
implements Pool, Disposable
{
private boolean m_disposed = false;
private final Buffer m_buffer;
private final ObjectFactory m_factory;
private final long m_timeout;
private final int m_maxSize;
/** The semaphor we synchronize on */
protected final Object m_semaphore = new Object();
public BlockingFixedSizePool( ObjectFactory factory, int size )
throws Exception
{
this( factory, size, 1000 );
}
public BlockingFixedSizePool( ObjectFactory factory, int size, long timeout )
throws Exception
{
m_timeout = (timeout < 1) ? 0 : timeout;
m_buffer = new FixedSizeBuffer( size );
m_maxSize = size;
m_factory = factory;
for( int i = 0; i < size; i++ )
{
m_buffer.add( newInstance() );
}
}
public Object acquire()
{
if( m_disposed )
{
throw new IllegalStateException( "Cannot get an object from a disposed
pool" );
}
Object object = null;
synchronized( m_semaphore )
{
if ( m_buffer.isEmpty() )
{
long blockStart = System.currentTimeMillis();
if ( m_timeout > 0 )
{
long blockWait = m_timeout;
do
{
if ( blockWait > 0 )
{
try
{
m_semaphore.wait( blockWait );
}
catch ( InterruptedException ie )
{}
if ( m_disposed )
{
throw new IllegalStateException( "Pool disposed of
while waiting for resources to free up" );
}
if ( m_buffer.isEmpty() )
{
blockWait = m_timeout -
( System.currentTimeMillis() - blockStart );
}
}
} while ( m_buffer.isEmpty() );
}
else
{
do
{
try
{
m_semaphore.wait();
}
catch (InterruptedException ie)
{}
if ( m_disposed )
{
throw new IllegalStateException( "Pool disposed of while
waiting for resources to free up" );
}
} while ( m_buffer.isEmpty() );
}
object = m_buffer.remove();
}
else
{
object = m_buffer.remove();
}
}
if ( object == null )
{
throw new IllegalStateException("Timeout exceeded without acquiring
resource.");
}
return object;
}
public void release( Object object )
{
synchronized( m_semaphore )
{
if ( m_disposed )
{
try
{
m_factory.dispose( object );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if it
happens
}
}
else
{
if ( m_buffer.size() < m_maxSize )
{
m_buffer.add( object );
m_semaphore.notify();
}
else
{
try
{
m_factory.dispose( object );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if it
happens
}
}
}
}
}
public Object newInstance()
throws Exception
{
return m_factory.newInstance();
}
public void dispose()
{
m_disposed = true;
synchronized( m_semaphore )
{
while( ! m_buffer.isEmpty() )
{
try
{
m_factory.dispose( m_buffer.remove() );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if it
happens
}
}
m_semaphore.notifyAll();
}
}
}
1.3 +25 -11
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/thread/impl/DefaultThreadPool.java
Index: DefaultThreadPool.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/thread/impl/DefaultThreadPool.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultThreadPool.java 9 Aug 2002 05:32:03 -0000 1.2
+++ DefaultThreadPool.java 9 Aug 2002 19:01:12 -0000 1.3
@@ -55,13 +55,14 @@
import org.apache.avalon.framework.logger.Logger;
import org.apache.excalibur.threadcontext.ThreadContext;
import org.apache.excalibur.mpool.ObjectFactory;
-import org.apache.excalibur.mpool.FixedSizePool;
+import org.apache.excalibur.mpool.BlockingFixedSizePool;
import org.apache.excalibur.thread.ThreadControl;
import org.apache.excalibur.thread.ThreadPool;
/**
* This class is the public frontend for the thread pool code.
*
+ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
*/
@@ -69,7 +70,7 @@
extends ThreadGroup
implements ObjectFactory, LogEnabled, Disposable, ThreadPool
{
- private FixedSizePool m_pool;
+ private BlockingFixedSizePool m_pool;
private int m_level;
@@ -86,16 +87,29 @@
public DefaultThreadPool( final String name, final int capacity )
throws Exception
{
- this( name, capacity, null );
+ this( name, capacity, null, 1000 );
+ }
+
+ public DefaultThreadPool( final String name, final int capacity, final int
timeout )
+ throws Exception
+ {
+ this( name, capacity, null, timeout );
+ }
+
+ public DefaultThreadPool( final String name, final int capacity, final
ThreadContext context )
+ throws Exception
+ {
+ this( name, capacity, context, 1000 );
}
public DefaultThreadPool( final String name,
final int capacity,
- final ThreadContext context )
+ final ThreadContext context,
+ final int timeout)
throws Exception
{
super( name );
- m_pool = new FixedSizePool( this, capacity );
+ m_pool = new BlockingFixedSizePool( this, capacity, timeout );
m_context = context;
}
@@ -172,13 +186,13 @@
*/
protected WorkerThread getWorker()
{
- try
- {
- return (WorkerThread)m_pool.acquire();
- }
- catch( final Exception e )
+ final WorkerThread thread = (WorkerThread)m_pool.acquire();
+
+ if ( null == thread )
{
- throw new IllegalStateException( "Unable to access thread pool due to "
+ e );
+ throw new IllegalStateException( "Unable to access thread pool due to
timeout exceeded" );
}
+
+ return thread;
}
}
1.6 +2 -1
jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/command/test/TPCThreadManagerTestCase.java
Index: TPCThreadManagerTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/event/command/test/TPCThreadManagerTestCase.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TPCThreadManagerTestCase.java 9 Aug 2002 05:32:03 -0000 1.5
+++ TPCThreadManagerTestCase.java 9 Aug 2002 19:01:12 -0000 1.6
@@ -117,7 +117,7 @@
threadManager.register( new Pipeline( result, errorOut ) );
// sleeps for 1 more scheduling timeout to surely go over limit
- Thread.sleep( SCHEDULING_TIMEOUT * ( MINIMAL_NUMBER_INVOCATIONS + 1) );
+ Thread.sleep( SCHEDULING_TIMEOUT * ( MINIMAL_NUMBER_INVOCATIONS + 1 ) );
int numberCalls = result.length();
@@ -178,6 +178,7 @@
public void handleEvents( QueueElement[] elements )
{
+System.out.println("handled event");
// records the fact that the handler was called
m_result.append( 'a' );
try
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>