common.threads
Class WorkerThreadPool

java.lang.Object
  extended bycommon.threads.WorkerThreadPool

public class WorkerThreadPool
extends java.lang.Object

A thread pool that handles requests (Runnable objects) and provides some extra functionality.

User's are encouraged to use the shared instance by calling getSharedInstance(), but they can instantiate new pools if they so desire. Just remember to shut down the threads when the plugin is unloaded.

This is a less featureful version of Java 5's "ThreadPoolExecutor" and "Future" classes, with the benefit that they run on 1.4.

Since:
CC 0.9.0
Author:
Marcelo Vanzin
See Also:
addRequest(Runnable), addRequests(Runnable[]), WorkRequest

Constructor Summary
WorkerThreadPool()
           
 
Method Summary
 WorkRequest addRequest(java.lang.Runnable req)
          Adds a request to the pool.
 WorkRequest[] addRequests(java.lang.Runnable[] reqs)
          Makes sure that there are enough threads to execute all requests in parallel and enqueue the requests.
 void ensureCapacity(int size)
          Ensures that at least size threads are available to handle requests.
static WorkerThreadPool getSharedInstance()
           
 WorkRequest[] runRequests(java.lang.Runnable[] reqs)
          Immediately runs the given requests.
 void shutdown()
          Asks all running threads to shutdown.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WorkerThreadPool

public WorkerThreadPool()
Method Detail

getSharedInstance

public static WorkerThreadPool getSharedInstance()

addRequest

public WorkRequest addRequest(java.lang.Runnable req)
Adds a request to the pool. The request will be run in the first available thread after it becomes available. This method will not block; it will just enqueue the request.

If no threads have yet been started, a new thread is created and started.


addRequests

public WorkRequest[] addRequests(java.lang.Runnable[] reqs)
Makes sure that there are enough threads to execute all requests in parallel and enqueue the requests. It's not guaranteed that all the requests will run in parallel, but the pool is guaranteed to have enough running threads to do so.

So if you enqueue 4 requests and there are 4 idle threads, each request will run on a separate thread. But if one thread is running a long request, it may happen that one of the new requests might finish before that running job, and another one of the new requests will run on that same thread.


runRequests

public WorkRequest[] runRequests(java.lang.Runnable[] reqs)
Immediately runs the given requests. If not enough worker threads are free to handle all the requests, new threads are created to be able to handle the new requests.

Since:
CC 0.9.1

ensureCapacity

public void ensureCapacity(int size)
Ensures that at least size threads are available to handle requests.


shutdown

public void shutdown()
Asks all running threads to shutdown.