Sathish Kumar
Sathish Kumar

Reputation: 456

How to detect if there is any idle thread in a ThreadPool?

I created a threadpool with the value of 5. After assigning the tasks to 5 threads, the worker should wait to create the thread until a thread get free in Threadpool. So, how to find, is any thread is free in a threadpool?

Thanks in advance....

Upvotes: 1

Views: 1603

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 19622

if (thread instanceof ThreadPoolExecutor) {
    System.out.println(
        "Pool size is now " +
        ((ThreadPoolExecutor) pool).getActiveCount()
    );
}

Taking into Consideration that the name of the ExecutorService is thread, this provides you the active thread and now you can get the free threads.

Upvotes: 2

Related Questions