Java Executors newCachedThreadPool() Method

The newCachedThreadPool() method of Executors class creates a thread pool that creates new threads as needed but will reuse previously constructed threads when they are available.

Syntax

Parameter

threadFactory - the factory to use when creating new threads

Returns

the newly created thread pool

Throws

NullPointerException

Example 1

Test it Now

Output:

size of mypool: 0
Total number threads scheduled): 2
Thread Name: pool-1-thread-2
Thread Name: pool-1-thread-1
after sleep Thread Name: pool-1-thread-2
after sleep Thread Name: pool-1-thread-1

Example 2

Test it Now

Output:

Thread Name: pool-1-thread-4
after sleep Thread Name: pool-1-thread-4
Thread Name: pool-1-thread-3
after sleep Thread Name: pool-1-thread-3
Thread Name: pool-1-thread-1
after sleep Thread Name: pool-1-thread-1
Next TopicJava Executors




Latest Courses