- You need to define a size for your Thread Pool, and a maximum queue capacity (maximum number of tasks enqueued).
IE:
framework.pool.ThreadPool threadPool = new framework.pool.FixedSizeThreadPool(3, 2000). For now, FixedThreadPool is the only implementation, but in the future there may be alternatives like an auto resizing framework pool. This must be done using try-with-resources, since the framework.pool.ThreadPool implements the AutoCloseable interface. Example:
try(framework.pool.ThreadPool threadPool = new framework.pool.FixedSizeThreadPool(3, 2000)) {
// Your code here, tipically submission of tasks as detailed below.
} - You need to submit tasks for your Thread Pool. This can be done either via lambda, using anonymous classes:
threadPool.submitTask(() -> {
System.out.println("Starting task");
Thread.sleep(1000l);
System.out.println("Ending task");
return UUID.randomUUID();
});Or in a more traditional manner, with classes implementing the framework.tasks.Task interface:
import framework.tasks.Task;
public class framework.tasks.HelloWorldTask implements Task<String> {
@Override
public String run() throws InterruptedException {
return "Hello World";
}
}
//...
threadPool.submitTask(new framework.tasks.HelloWorldTask());- After all submitted tasks have been concluded, your framework.pool.ThreadPool will automatically be deleted, along with all it's threads.
