public class ThreadPool extends Object
Runnable tasks in a number of threads (over a number of CPU cores).
Usage:
ThreadPool pool = new ThreadPool();
// optional
pool.setThreadNamePrefix("foo"); // for debugger output
pool.setThreadCount(5);
// add one or more seed tasks
pool.addTask(new Runnable() { ... });
// start threads, execute the seed tasks, and execute any tasks they create
pool.execute();
In the case that any task throws an exception, this exception is thrown by the execute() method.
If all tasks run to completion, the execute() method returns with no value.
Tasks can depend on other tasks. Use the addTaskWithDependencies(List, Runnable) method to add a new task,
which will only start in the first parameter after all the tasks in the second parameter have run to completion. All tasks in the List should have been
previously added using the normal addTask(Runnable...) method, or themselves with addTaskWithDependencies(List, Runnable).
The difference to an ExecutorService is:
ExecutorService the client adds a fixed number of tasks,
and they are then executed, without the executing tasks being able to add more tasks).
Such functionality is mandatory for web crawlers, which, during the processing of pages, discover links which
point to additional pages which require processing.ExecutorService.
If the execute() method is never called then no threads are ever started and the object can be garbage collected normally.
If the execute() method is called then that method makes sure all threads it creates are destroyed.
| Modifier and Type | Class and Description |
|---|---|
protected class |
ThreadPool.RunnerRunnable |
protected static class |
ThreadPool.TaskWithDependencies |
| Modifier and Type | Field and Description |
|---|---|
protected Map<Runnable,List<ThreadPool.TaskWithDependencies>> |
blockerTasks |
protected Exception |
exceptionOrNull |
protected com.databasesandlife.util.IdentityHashSet<Runnable> |
executingTasks |
protected com.databasesandlife.util.IdentityHashSet<Runnable> |
readyTasks |
protected int |
threadCount |
protected String |
threadNamePrefix |
| Constructor and Description |
|---|
ThreadPool() |
| Modifier and Type | Method and Description |
|---|---|
void |
addTask(Runnable... tasks) |
void |
addTasks(Collection<Runnable> tasks) |
void |
addTaskWithDependencies(List<Runnable> dependencies,
Runnable r) |
void |
execute() |
void |
setThreadCount(int count) |
void |
setThreadNamePrefix(String prefix) |
protected int threadCount
protected final com.databasesandlife.util.IdentityHashSet<Runnable> readyTasks
protected final com.databasesandlife.util.IdentityHashSet<Runnable> executingTasks
protected final Map<Runnable,List<ThreadPool.TaskWithDependencies>> blockerTasks
@CheckForNull protected Exception exceptionOrNull
public void setThreadCount(int count)
public void setThreadNamePrefix(String prefix)
public void addTaskWithDependencies(List<Runnable> dependencies, Runnable r)
public void addTask(Runnable... tasks)
public void addTasks(Collection<Runnable> tasks)
public void execute()
Copyright © 2003–2018. All rights reserved.