OnJava8-Examples/concurrent/CachedThreadPool.java

30 lines
798 B
Java
Raw Normal View History

2016-11-23 09:05:26 -08:00
// concurrent/CachedThreadPool.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2016-07-05 14:46:09 -06:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-05 14:46:09 -06:00
import java.util.concurrent.*;
2016-12-21 11:06:49 -08:00
import java.util.stream.*;
2016-07-05 14:46:09 -06:00
public class CachedThreadPool {
2016-12-21 11:06:49 -08:00
public static void main(String[] args) {
2016-07-05 14:46:09 -06:00
ExecutorService exec =
Executors.newCachedThreadPool();
2016-12-21 11:06:49 -08:00
IntStream.range(0, 10)
.mapToObj(NapTask::new)
.forEach(exec::execute);
2016-07-05 14:46:09 -06:00
exec.shutdown();
}
}
/* Output:
NapTask[7] pool-1-thread-8
NapTask[4] pool-1-thread-5
NapTask[1] pool-1-thread-2
NapTask[3] pool-1-thread-4
NapTask[0] pool-1-thread-1
NapTask[8] pool-1-thread-9
NapTask[2] pool-1-thread-3
NapTask[9] pool-1-thread-10
NapTask[6] pool-1-thread-7
NapTask[5] pool-1-thread-6
2016-07-05 14:46:09 -06:00
*/