org.sapia.ubik.net
Class ThreadPool

java.lang.Object
  extended by org.sapia.ubik.net.Pool
      extended by org.sapia.ubik.net.ThreadPool
Direct Known Subclasses:
SocketRmiServerThreadPool

public abstract class ThreadPool
extends Pool

A pool of PooledThread instances. Inheriting classes must implement the newThread() method, which must return an application-specific PooledThread instance.

Applications must use the pool in the following manner:


 PooledThread thread = (PooledThread)threadPool.acquire();

 thread.exec(someData);

 
Upon the exec() method being called, the thread immediately:

Thus, applications need not be concerned about returning the passed in thread to the pool.

Author:
Yanick Duchesne
Copyright:
Copyright © 2002-2003 Sapia Open Source Software. All Rights Reserved.
License:
Read the license.txt file of the jar or visit the license page at the Sapia OSS web site
See Also:
PooledThread, PooledThread.doExec(Object)

Field Summary
 
Fields inherited from class org.sapia.ubik.net.Pool
_currentCount, _lastUsageTime, _maxSize, _objects, NO_MAX, NO_TIME_OUT
 
Constructor Summary
protected ThreadPool(java.lang.String name, boolean daemon)
          Creates a thread pool.
protected ThreadPool(java.lang.String name, boolean daemon, int maxSize)
          Creates a thread pool.
 
Method Summary
 void disableStats()
          Disables statistics
protected  java.lang.Object doNewObject()
          This template method should be overridden by inheriting classes to provide object instances that will be pooled.
 void enabledStats()
          Enables statistics
 Statistic getDurationStat()
           
 Statistic getRpsStat()
           
 int getThreadCount()
           
protected abstract  PooledThread newThread()
          This method must be overridden by inheriting classes; the returned thread must not be started by this method; the pool implements this behavior.
protected  java.lang.Object onAcquire(java.lang.Object o)
           
protected  void onRelease(java.lang.Object o)
           
 void shutdown()
          Cleanly shuts down this instance.
 void shutdown(long timeout)
          Cleanly shuts down this instance; internally busy threads are interrupted - currently executing threads finish their task before termination.
 
Methods inherited from class org.sapia.ubik.net.Pool
acquire, acquire, acquireCreate, cleanup, fill, getCreatedCount, getLastUsageTime, release, shrinkTo, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

protected ThreadPool(java.lang.String name,
                     boolean daemon,
                     int maxSize)
Creates a thread pool.

Parameters:
name - the name of the threads that will be created (to distinguish among the different threads, a counter is appended to the name for each thread).
daemon - if true, the threads created by this pool will be set as daemon.
maxSize - the maximum number of threads that this pool creates.

ThreadPool

protected ThreadPool(java.lang.String name,
                     boolean daemon)
Creates a thread pool.

Parameters:
name - the name of the threads that will be created (to distinguish among the different threads, a counter is appended to the name for each thread).
daemon - if true, the threads created by this pool will be set as daemon.
Method Detail

onAcquire

protected java.lang.Object onAcquire(java.lang.Object o)
                              throws java.lang.Exception,
                                     java.lang.IllegalStateException
Overrides:
onAcquire in class Pool
Throws:
java.lang.IllegalStateException - if this instance is shutting down or is shut down.
java.lang.Exception
See Also:
Pool.onAcquire(Object)

onRelease

protected void onRelease(java.lang.Object o)
Overrides:
onRelease in class Pool
See Also:
Pool.onRelease(Object)

getRpsStat

public Statistic getRpsStat()
Returns:
this instance's requests-per-second stat.

getDurationStat

public Statistic getDurationStat()
Returns:

enabledStats

public void enabledStats()
Enables statistics


disableStats

public void disableStats()
Disables statistics


shutdown

public void shutdown()
Cleanly shuts down this instance.

See Also:
shutdown(long)

getThreadCount

public int getThreadCount()
Returns:
the number of active threads in this instance.

shutdown

public void shutdown(long timeout)
Cleanly shuts down this instance; internally busy threads are interrupted - currently executing threads finish their task before termination.

This method waits that all threads are finished before it returns, OR until the given timeout is reached.

Parameters:
timeout - a shutdown timeout - this method will return when this timeout is reached, even if some threads are still executing.

doNewObject

protected final java.lang.Object doNewObject()
                                      throws java.lang.Exception
Description copied from class: Pool
This template method should be overridden by inheriting classes to provide object instances that will be pooled.

Specified by:
doNewObject in class Pool
Returns:
an Object to pool.
Throws:
java.lang.Exception - if an error occurs while creating the object to be returned.
See Also:
Pool.doNewObject()

newThread

protected abstract PooledThread newThread()
                                   throws java.lang.Exception
This method must be overridden by inheriting classes; the returned thread must not be started by this method; the pool implements this behavior.

Returns:
a PooledThread instance.
Throws:
java.lang.Exception


Copyright © 2010 Sapia OSS. All Rights Reserved.