Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.mortbay.thread
Class QueuedThreadPool

    java.lang.Object
      org.mortbay.component.AbstractLifeCycle
          org.mortbay.thread.QueuedThreadPool

All Implemented Interfaces:
  java.io.Serializable, LifeCycle, ThreadPool

    public class QueuedThreadPool

extends AbstractLifeCycle
implements java.io.Serializable, ThreadPool

A pool of threads.

Avoids the expense of thread creation by pooling threads after their run methods exit for reuse.

If an idle thread is available a job is directly dispatched, otherwise the job is queued. After queuing a job, if the total number of threads is less than the maximum pool size, a new thread is spawned.

Author:
  Greg Wilkins
See Also:
  Serialized Form

Nested Class Summary
`class` | `QueuedThreadPool.PoolThread` Pool Thread class.

Nested classes/interfaces inherited from interface org.mortbay.component.LifeCycle
`LifeCycle.Listener`

Field Summary

Fields inherited from class org.mortbay.component.AbstractLifeCycle
`_listeners`

Constructor Summary
`QueuedThreadPool()`
`QueuedThreadPool(int maxThreads)`

Method Summary
`boolean` | `dispatch(java.lang.Runnable job)` Run job.
`protected void` | `doStart()`
`protected void` | `doStop()` Stop the BoundedThreadPool.
`java.lang.String` | `dump()`
`int` | `getIdleThreads()` Get the number of idle threads in the pool.
`int` | `getLowThreads()`
`int` | `getMaxIdleTimeMs()` Get the maximum thread idle time.
`int` | `getMaxQueued()`
`int` | `getMaxStopTimeMs()`
`int` | `getMaxThreads()` Set the maximum number of threads.
`int` | `getMinThreads()` Get the minimum number of threads.
`java.lang.String` | `getName()`
`int` | `getQueueSize()`
`int` | `getSpawnOrShrinkAt()`
`int` | `getThreads()` Get the number of threads in the pool.
`int` | `getThreadsPriority()` Get the priority of the pool threads.
`boolean` | `interruptThread(java.lang.String name)`
`boolean` | `isDaemon()` Delegated to the named or anonymous Pool.
`boolean` | `isLowOnThreads()`
`void` | `join()` Blocks until the thread pool is `stopped`.
`protected void` | `newThread()`
`void` | `setDaemon(boolean daemon)` Delegated to the named or anonymous Pool.
`void` | `setLowThreads(int lowThreads)`
`void` | `setMaxIdleTimeMs(int maxIdleTimeMs)` Set the maximum thread idle time.
`void` | `setMaxStopTimeMs(int stopTimeMs)`
`void` | `setMaxThreads(int maxThreads)` Set the maximum number of threads.
`void` | `setMinThreads(int minThreads)` Set the minimum number of threads.
`void` | `setName(java.lang.String name)`
`void` | `setSpawnOrShrinkAt(int spawnOrShrinkAt)`
`void` | `setThreadsPriority(int priority)` Set the priority of the pool threads.
`protected void` | `stopJob(java.lang.Thread thread, java.lang.Object job)` Stop a Job.
`boolean` | `stopThread(java.lang.String name)`

Methods inherited from class org.mortbay.component.AbstractLifeCycle
`addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### QueuedThreadPool

    public QueuedThreadPool()

### QueuedThreadPool

    public QueuedThreadPool(int maxThreads)

Method Detail

### dispatch

    public boolean dispatch(java.lang.Runnable job)

  Run job.

  Specified by:
    `dispatch` in interface `ThreadPool`
  Returns:
    true

### getIdleThreads

    public int getIdleThreads()

  Get the number of idle threads in the pool.

  Specified by:
    `getIdleThreads` in interface `ThreadPool`
  Returns:
    Number of threads
  See Also:
    `getThreads()`

### getLowThreads

    public int getLowThreads()

  Returns:
    low resource threads threshhold

### getMaxQueued

    public int getMaxQueued()

  Returns:
    maximum queue size

### getMaxIdleTimeMs

    public int getMaxIdleTimeMs()

  Get the maximum thread idle time. Delegated to the named or anonymous Pool.

  Returns:
    Max idle time in ms.
  See Also:
    `setMaxIdleTimeMs(int)`

### getMaxThreads

    public int getMaxThreads()

  Set the maximum number of threads. Delegated to the named or anonymous Pool.

  Returns:
    maximum number of threads.
  See Also:
    `setMaxThreads(int)`

### getMinThreads

    public int getMinThreads()

  Get the minimum number of threads. Delegated to the named or anonymous Pool.

  Returns:
    minimum number of threads.
  See Also:
    `setMinThreads(int)`

### getName

    public java.lang.String getName()

  Returns:
    The name of the BoundedThreadPool.

### getThreads

    public int getThreads()

  Get the number of threads in the pool.

  Specified by:
    `getThreads` in interface `ThreadPool`
  Returns:
    Number of threads
  See Also:
    `getIdleThreads()`

### getThreadsPriority

    public int getThreadsPriority()

  Get the priority of the pool threads.

  Returns:
    the priority of the pool threads.

### getQueueSize

    public int getQueueSize()

### getSpawnOrShrinkAt

    public int getSpawnOrShrinkAt()

  Returns:
    the spawnOrShrinkAt The number of queued jobs (or idle threads) needed before the thread pool is grown (or shrunk)

### setSpawnOrShrinkAt

    public void setSpawnOrShrinkAt(int spawnOrShrinkAt)

  Parameters:
    `spawnOrShrinkAt` - The number of queued jobs (or idle threads) needed before the thread pool is grown (or shrunk)

### getMaxStopTimeMs

    public int getMaxStopTimeMs()

  Returns:
    maximum total time that stop() will wait for threads to die.

### setMaxStopTimeMs

    public void setMaxStopTimeMs(int stopTimeMs)

  Parameters:
    `stopTimeMs` - maximum total time that stop() will wait for threads to die.

### isDaemon

    public boolean isDaemon()

  Delegated to the named or anonymous Pool.

### isLowOnThreads

    public boolean isLowOnThreads()

  Specified by:
    `isLowOnThreads` in interface `ThreadPool`
  Returns:
    True if the pool is low on threads

### join

    public void join()
              throws java.lang.InterruptedException

  Description copied from interface: `ThreadPool`
  Blocks until the thread pool is `stopped`.

  Specified by:
    `join` in interface `ThreadPool`
  Throws:
    `java.lang.InterruptedException`

### setDaemon

    public void setDaemon(boolean daemon)

  Delegated to the named or anonymous Pool.

### setLowThreads

    public void setLowThreads(int lowThreads)

  Parameters:
    `lowThreads` - low resource threads threshhold

### setMaxIdleTimeMs

    public void setMaxIdleTimeMs(int maxIdleTimeMs)

  Set the maximum thread idle time. Threads that are idle for longer than this period may be stopped. Delegated to the named or anonymous Pool.

  Parameters:
    `maxIdleTimeMs` - Max idle time in ms.
  See Also:
    `getMaxIdleTimeMs()`

### setMaxThreads

    public void setMaxThreads(int maxThreads)

  Set the maximum number of threads. Delegated to the named or anonymous Pool.

  Parameters:
    `maxThreads` - maximum number of threads.
  See Also:
    `getMaxThreads()`

### setMinThreads

    public void setMinThreads(int minThreads)

  Set the minimum number of threads. Delegated to the named or anonymous Pool.

  Parameters:
    `minThreads` - minimum number of threads
  See Also:
    `getMinThreads()`

### setName

    public void setName(java.lang.String name)

  Parameters:
    `name` - Name of the BoundedThreadPool to use when naming Threads.

### setThreadsPriority

    public void setThreadsPriority(int priority)

  Set the priority of the pool threads.

  Parameters:
    `priority` - the new thread priority.

### doStart

    protected void doStart()
                    throws java.lang.Exception

  Overrides:
    `doStart` in class `AbstractLifeCycle`
  Throws:
    `java.lang.Exception`

### doStop

    protected void doStop()
                   throws java.lang.Exception

  Stop the BoundedThreadPool. New jobs are no longer accepted,idle threads are interrupted and stopJob is called on active threads. The method then waits min(getMaxStopTimeMs(),getMaxIdleTimeMs()), for all jobs to stop, at which time killJob is called.

  Overrides:
    `doStop` in class `AbstractLifeCycle`
  Throws:
    `java.lang.Exception`

### newThread

    protected void newThread()

### stopJob

    protected void stopJob(java.lang.Thread thread,
                           java.lang.Object job)

  Stop a Job. This method is called by the Pool if a job needs to be stopped. The default implementation does nothing and should be extended by a derived thread pool class if special action is required.

  Parameters:
    `thread` - The thread allocated to the job, or null if no thread allocated.
    `job` - The job object passed to run.

### dump

    public java.lang.String dump()

### stopThread

    public boolean stopThread(java.lang.String name)

  Parameters:
    `name` - The thread name to stop.
  Returns:
    true if the thread was found and stopped.

### interruptThread

    public boolean interruptThread(java.lang.String name)

  Parameters:
    `name` - The thread name to interrupt.
  Returns:
    true if the thread was found and interrupted.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 1995-2010 Mort Bay Consulting. All Rights Reserved.