public class WorkerThreadPool
extends java.lang.Object
Users 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 version of Java 5's java.util.concurrent.ThreadPoolExecutor with one addition: addRequests() will start the runnables at the same time, or wait until size threads are available in the pool.
ThreadUtilities
Constructor and Description |
---|
WorkerThreadPool() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static WorkerThreadPool getSharedInstance()
public WorkRequest addRequest(java.lang.Runnable req)
If no threads have yet been started, a new thread is created and started.
public WorkRequest[] addRequests(java.lang.Runnable[] reqs)
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.
public WorkRequest[] runRequests(java.lang.Runnable[] reqs)
public void ensureCapacity(int size)
size
threads are available to
handle requests.public void shutdown()