public class ThreadPool extends Object
Modifier | Constructor and Description |
---|---|
|
ThreadPool(int maxThreads)
Creates a new ThreadPool with a maximum number of threads.
|
protected |
ThreadPool(int maxThreads,
ThreadPool parent)
Creates a new sub pool.
|
Modifier and Type | Method and Description |
---|---|
ThreadPool |
createSubPool()
Creates a sub pool that shares the threads with this (parent) pool.
|
ThreadPool |
createSubPool(int maxThreads)
Creates a sub pool that shares the threads with this (parent) pool.
|
static ThreadPool |
currentPool()
If the current thread is taken out of a thread pool, this method will
return the thread pool.
|
<T> Future<T> |
enqueue(Callable<T> task)
Submits a value-returning task for execution and returns a Future
representing the pending results of the task.
|
Future<?> |
enqueue(Runnable r)
Submits a Runnable task for execution and returns a Future representing
that task.
|
protected void |
finalize() |
int |
getMaxThreads()
Returns the maximum number of threads in the pool.
|
(package private) int |
getQueueSize()
Returns the size of the future queue (needed by the unit test).
|
int |
getRunningThreads()
Returns the number of currently running threads in this pool and its sub
pools.
|
void |
interruptAll()
Interrupts all running jobs.
|
<T> T |
runInvisible(Callable<T> r)
Executes the runnable in the current thread.
|
void |
setMaxThreads(int newValue)
Sets the maximum number of threads in the pool.
|
void |
shutdown()
Shuts the pool down, still running threads are not interrupted.
|
<T> Future<T> |
submit(Callable<T> task)
Submits a value-returning task for execution and returns a Future
representing the pending results of the task.
|
Future<?> |
submit(Runnable task)
Submits a Runnable task for execution and returns a Future representing
that task.
|
<T> Future<T> |
trySubmit(Callable<T> t)
Tries to submits a value-returning task for immediate execution and
returns a Future representing the pending results of the task if a thread
is free.
|
Future<?> |
trySubmit(Runnable r)
Tries to submits a Runnable task for immediate execution and returns a
Future representing the task if a thread is free.
|
void |
waitForTermination()
Waits until all jobs in this pool and its sub pools have been finished.
|
protected void |
workerFinished(org.knime.core.util.ThreadPool.Worker w)
This method is called every time a worker has finished.
|
public ThreadPool(int maxThreads)
maxThreads
- the maximum number of threadsprotected ThreadPool(int maxThreads, ThreadPool parent)
maxThreads
- the maximum number of threads in the poolparent
- the parent poolpublic ThreadPool createSubPool()
public ThreadPool createSubPool(int maxThreads)
maxThreads
- the maximum number of threads in the sub poolpublic <T> Future<T> enqueue(Callable<T> task)
If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();
Note: The Executors
class includes a set of
methods that can convert some other common closure-like objects, for
example, PrivilegedAction
to Callable
form
so they can be submitted.
T
- any result typetask
- the task to submitNullPointerException
- if task
nullsubmit(Callable)
public Future<?> enqueue(Runnable r)
r
- the task to submitNullPointerException
- if task
nullsubmit(Runnable)
public <T> Future<T> trySubmit(Callable<T> t)
null
is
returned and the task is not queued.
If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();
Note: The Executors
class includes a set of
methods that can convert some other common closure-like objects, for
example, PrivilegedAction
to Callable
form
so they can be submitted.
T
- any result typet
- the task to submitnull
if no thread was availableNullPointerException
- if task
nullsubmit(Callable)
public Future<?> trySubmit(Runnable r)
null
is returned and the task is not
queued.r
- the task to submitNullPointerException
- if task
nullsubmit(Runnable)
public int getMaxThreads()
public int getRunningThreads()
public <T> T runInvisible(Callable<T> r) throws ExecutionException
T
- Type of the argument (result type)r
- A callable, which will be executed by the thread invoking this
method.IllegalThreadStateException
- if the current thread is not taken
out of a thread poolExecutionException
- if the callable could not be executed for some
reasonpublic void setMaxThreads(int newValue)
newValue
- the new maximum thread numberpublic void shutdown()
public void interruptAll()
public <T> Future<T> submit(Callable<T> task) throws InterruptedException
If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();
Note: The Executors
class includes a set of
methods that can convert some other common closure-like objects, for
example, PrivilegedAction
to Callable
form
so they can be submitted.
T
- any result typetask
- the task to submitInterruptedException
- if the thread is interruptedenqueue(Callable)
public Future<?> submit(Runnable task) throws InterruptedException
task
- the task to submitInterruptedException
- if the thread is interruptedenqueue(Runnable)
public void waitForTermination() throws InterruptedException
InterruptedException
- if the thread is interrupted while waitingprotected void workerFinished(org.knime.core.util.ThreadPool.Worker w)
w
- the finished workerprotected void finalize() throws Throwable
public static ThreadPool currentPool()
null
.null
int getQueueSize()
KNIME GmbH, Konstanz, Germany
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.