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(5000); // Reading file
System.out.println("Read file successfully: "+fileName+" by "+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getFileName() {
return fileName;
}
}
|
Let’s create ThreadPoolExecutor which will consume above task and process it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package org.arpit.java2blog;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolExecutorMain {
public static void main(String args[]) {
// Getting instance of ThreadPoolExecutor using Executors.newCachedThreadPool factory method
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
for (int i = 1; i <= 10; i++) {
FetchDataFromFile fdff = new FetchDataFromFile("File :" + i);
System.out.println("A new file has been added to read : " + fdff.getFileName());
threadPoolExecutor.execute(fdff);
}
threadPoolExecutor.shutdown();
}
}
|
When you run above program, you will get below output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
A new file has been added to read : File :1
A new file has been added to read : File :2
Fetching data from File :1 by pool-1-thread-1
Fetching data from File :2 by pool-1-thread-2
A new file has been added to read : File :3
A new file has been added to read : File :4
Fetching data from File :3 by pool-1-thread-3
Fetching data from File :4 by pool-1-thread-4
A new file has been added to read : File :5
Fetching data from File :5 by pool-1-thread-5
A new file has been added to read : File :6
Fetching data from File :6 by pool-1-thread-6
A new file has been added to read : File :7
Fetching data from File :7 by pool-1-thread-7
A new file has been added to read : File :8
A new file has been added to read : File :9
Fetching data from File :8 by pool-1-thread-8
A new file has been added to read : File :10
Fetching data from File :9 by pool-1-thread-9
Fetching data from File :10 by pool-1-thread-10
|
If you notice, we have submitted 10 tasks and it has created 10 new threads depending on demand.If any thread remains idle for more than one minute, it will tear it down. newCachedThreadPool is a good choice when you want better queuing performance than newFixedThreadPool. If you want to restrict number of concurrent task for resource management, go with newFixedThreadPool.