Java newCachedThreadPool Example
GIAN Tutorials
8:16 AM
Executor’s newCachedThreadPool factory method : This method returns an unbounded thread pool. It sets maximum pool size to Integer.Max and it will create new threads depending on demand. If demand decreases, it will tear down threads if threads are idle for more than 1 min. Example: Let’s create a Task. Here Task will be to read different files and process them. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package org . arpit . java2blog . bean ; public class FetchDataFromFile implements Runnable { private final String fileName ; public FetchDataFromFile ( String fileName ) { super ( ) ; this . fileName = fileName ; } @ Override public void run ( ) { try { System . out . println ( "Fetching data from " + fileName + " by " + Thread . currentThread ( ) . getName ( ) ) ; Thread . sleep ( 5