M
M
Mad Moon2021-07-15 13:47:34
Java
Mad Moon, 2021-07-15 13:47:34

How to set maximum queue time using Thread Pool Executor?

Is there a way to set a time limit for each request from a queue to a pool?

I have a class that inherits ThreadPoolExecutor

public class TaskPool extends ThreadPoolExecutor {

public TaskPool (int initialCountOfThreads, int maxPollSize) {
    super(initialCountOfThreads, maxPollSize, 100, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(150));
  }

I am adding tasks to the thread pool
TaskPool taskPool = new TaskPool(2, 50);
for (int i = 0; i < 50000000; i++) {
    task = new Task("Test" + i);
    taskPool.execute(task);
}

And all the tasks that are in the queue are there until it is full, to be exact, up to 150, and I want the tasks in the queue to be deleted after some period of time if they are not included in the pool

[email protected]2d6a9952[Running, pool size = 50, active threads = 48, queued tasks = 150, completed tasks = 7430]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question