Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Java 8 fixed thread pool always create a fixed number threads?

I'm looking at code for the Java 8 fixed thread pool. Here's a small sample:

    ExecutorService service = Executors.newFixedThreadPool(100);

    try {

        for (int i = 0; i < 10; ++i) {
            service.submit(() -> "In another thread!");
        }

    } catch (Exception e) {
        // Do nothing
    } finally {
        service.shutdown();
    }

The question I have is (in two parts):

a. How many threads will the fixed thread pool object above create? 100? Or just 10?

b. How can I check how many threads have been created?

like image 308
Anupam Banerji Avatar asked Aug 11 '26 09:08

Anupam Banerji


1 Answers

How many threads will the fixed thread pool object above create? 100? Or just 10?

It isn't specified. All that is specified is that there will never be more than 100.

How can I check how many threads have been created?

There is no API for that. You could get the total thread count before and after, but that could be fuzzy as Java has other reasons for creating threads.

like image 103
user207421 Avatar answered Aug 12 '26 22:08

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!