The tomcat thread pool is nice.  I'm using it in several places.

We made a wrapper and an interface for using it.

import java.util.*;
import org.apache.tomcat.util.*;
/** Provides a singleton thread pool. */
public class ThreadPoolManager {
  /** The singleton thread pool manager. */
  private static final ThreadPoolManager singleton = new
ThreadPoolManager();

  /** The thread pool. */
  private ThreadPool pool = new ThreadPool();
  /** True iff the thread pool has been started. */
  private boolean started;
  /** Can only be constructed by this class. */
  private ThreadPoolManager() {}
  /**
   * Sets the max number of threads that you can open in the pool.
   * Will only be effective if called before the getInstance method is
invoked for the first time.
   */
  public static void setMaxThreads(int maxThreads) {
    singleton.pool.setMaxThreads(maxThreads);
  }
  /**
   * Sets the min number of idle threads that you can leave in the pool.
   * Will only be effective if called before the getInstance method is
invoked for the first time.
   */
  public static void setMinSpareThreads(int minSpareThreads) {
    singleton.pool.setMinSpareThreads(minSpareThreads);
  }
  /**
   * Sets the max number of idle threads that you can leave in the pool.
   * Will only be effective if called before the getInstance method is
invoked for the first time.
   */
  public static void setMaxSpareThreads(int maxSpareThreads) {
    singleton.pool.setMaxSpareThreads(maxSpareThreads);
  }
  /**
   * Gets the max number of threads that you can open in the pool.
   * Will only be accurate if called after the getInstance method is invoked
for the first time.
   */
  public static int getMaxThreads() {
    return singleton.pool.getMaxThreads();
  }
  /**
   * Gets the min number of idle threads that you can leave in the pool.
   * Will only be accurate if called after the getInstance method is invoked
for the first time.
   */
  public static int getMinSpareThreads() {
    return singleton.pool.getMinSpareThreads();
  }
  /**
   * Gets the max number of idle threads that you can leave in the pool.
   * Will only be accurate if called after the getInstance method is invoked
for the first time.
   */
  public static int getMaxSpareThreads() {
    return singleton.pool.getMaxSpareThreads();
  }
  /**
   * Returns the singleton thread pool manager, which can be used to execute
a
   * given IThreadPoolRunnable on a thread in the pool.
   * Configuration of the thread pool must be made prior to invoking this
method.
   */
  public static ThreadPoolManager getInstance() {
    if (!singleton.started) {
      synchronized(singleton) {
        if (!singleton.started) {
          singleton.pool.start();
          singleton.started = true;
        }
      }
    }
    return singleton;
  }
  /**
   * Shuts down the thread pool and re-initializes it to the default.
   */
  public static void reset() {
    synchronized(singleton) {
      if (singleton.started) {
        singleton.started = false;
        singleton.pool.shutdown();
      }
      singleton.pool = new ThreadPool();
    }
    return;
  }
  /**
   * Executes a given IThreadPoolRunnable on a thread in the pool, block if
needed.
   */
  public void runIt(IThreadPoolRunnable r) {
    pool.runIt(r);
  }
}



import org.apache.tomcat.util.*;

/** 
 * Implemented if you want to run a piece of code inside a thread pool.
 */
public interface IThreadPoolRunnable extends ThreadPoolRunnable {
    // Super-interface methods duplicated here for easy reference.
    /** 
     * Called when this object is first loaded in the thread pool.
     * Important: all workers in a pool must be of the same type,
     * otherwise the mechanism becomes more complex.
     */
    // public Object[] getInitData();
    /** 
     * This method will be executed in one of the pool's threads. The
     * thread will be returned to the pool.
     */
    // public void runIt(Object thData[]);
}


-----Original Message-----
From: Jeff Turner [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 06, 2001 12:04 AM
To: [EMAIL PROTECTED]
Subject: Re: [THREADING][POOL] ThreadPool?


Might want to have a look at:

jakarta-avalon/src/scratchpad/org/apache/avalon/excalibur/thread

DefaultThreadPool.java
ThreadContext.java
ThreadPool.java
WorkerThread.java

--Jeff


On Tue, Jun 05, 2001 at 04:42:21PM -0700, Hristo Stoyanov wrote:
> Hi-,
> I checked the POOL module and
> the THREADING module and I could not find
> any thread pool implementation. There are plenty
> out there, the most featured one
> being in 
> http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/
> 
> By the way, why was the Allen Holub's threading
> goodies
> choosen over those of Dough Lea (it does not seems to
> be as mature).
>  
> Hristo

Reply via email to