1.1.
Executorservice
Executorservice is the interface of the thread pool.
Executors is a tool class for creating different thread pools.
1.2.
thread pool with fixed number of threads
Executorservice executorservice = Executors.newfixedthreadpool (2for (int j=0;j< 10;j++) {finalint t =new Runnable () {@Overridepublic void run () {for (int i=0;i<4;i++) {System.out.println ( "T:" + t + "," + "I:" + i + ", Tid:" +
t:0,i:0, Tid:8
T:0,i:1, Tid:8
T:0,i:2, Tid:8
T:0,i:3, Tid:8
t:1,i:0, Tid:9
t:2,i:0, Tid:8
T:2,i:1, Tid:8
T:1,i:1, Tid:9
T:2,i:2, Tid:8
T:2,i:3, Tid:8
T:1,i:2, Tid:9
T:1,i:3, Tid:9
t:4,i:0, Tid:9
T:4,i:1, Tid:9
T:4,i:2, Tid:9
t:3,i:0, Tid:8
T:3,i:1, Tid:8
T:4,i:3, Tid:9
T:3,i:2, Tid:8
t:5,i:0, Tid:9
T:5,i:1, Tid:9
T:3,i:3, Tid:8
T:5,i:2, Tid:9
T:5,i:3, Tid:9
t:6,i:0, Tid:8
T:6,i:1, Tid:8
T:6,i:2, Tid:8
T:6,i:3, Tid:8
t:8,i:0, Tid:8
t:7,i:0, Tid:9
T:7,i:1, Tid:9
T:8,i:1, Tid:8
T:8,i:2, Tid:8
T:8,i:3, Tid:8
T:7,i:2, Tid:9
t:9,i:0, Tid:8
T:7,i:3, Tid:9
T:9,i:1, Tid:8
T:9,i:2, Tid:8
T:9,i:3, Tid:8
1.3.
thread pool with variable number of threads
Executorservice executorservice = executors.newcachedthreadpool (); for (int j=0;j<10;j++) {finalint t =new Runnable () {@ Overridepublicvoid run () {for (int i=0;i<4;i++ {System.out.println ("T:" + + t + "," + "I:" + i + ", tid:" + Thread.CurrentThread (). GetId ());}});}
t:2,i:0, Tid:10
T:2,i:1, Tid:10
t:3,i:0, Tid:11
T:3,i:1, Tid:11
t:0,i:0, Tid:8
T:0,i:1, Tid:8
T:0,i:2, Tid:8
T:0,i:3, Tid:8
t:1,i:0, Tid:9
T:1,i:1, Tid:9
T:1,i:2, Tid:9
T:3,i:2, Tid:11
T:1,i:3, Tid:9
T:2,i:2, Tid:10
T:2,i:3, Tid:10
t:5,i:0, Tid:13
T:5,i:1, Tid:13
t:9,i:0, Tid:17
t:4,i:0, Tid:12
T:4,i:1, Tid:12
T:9,i:1, Tid:17
T:9,i:2, Tid:17
T:5,i:2, Tid:13
T:5,i:3, Tid:13
t:8,i:0, Tid:16
T:8,i:1, Tid:16
T:8,i:2, Tid:16
T:8,i:3, Tid:16
t:7,i:0, tid:15
T:3,i:3, Tid:11
t:6,i:0, tid:14
T:7,i:1, tid:15
T:7,i:2, tid:15
T:7,i:3, tid:15
T:9,i:3, Tid:17
T:4,i:2, Tid:12
T:6,i:1, tid:14
T:6,i:2, tid:14
T:6,i:3, tid:14
T:4,i:3, Tid:12
1.4.
Thread pool for individual threads
Executorservice executorservice = executors.newsinglethreadexecutor (); for (int j=0;j<10;j++) {finalint t =new Runnable () {@ Overridepublicvoid run () {for (int i=0;i<4;i++ {System.out.println ("T:" + + t + "," + "I:" + i + ", tid:" + Thread.CurrentThread (). GetId ());}});}
The output information can be seen, the ( the thread ID (TID) is the same for each task (t) .)
t:0,i:0, Tid:8
T:0,i:1, Tid:8
T:0,i:2, Tid:8
T:0,i:3, Tid:8
t:1,i:0, Tid:8
T:1,i:1, Tid:8
T:1,i:2, Tid:8
T:1,i:3, Tid:8
t:2,i:0, Tid:8
T:2,i:1, Tid:8
T:2,i:2, Tid:8
T:2,i:3, Tid:8
t:3,i:0, Tid:8
T:3,i:1, Tid:8
T:3,i:2, Tid:8
T:3,i:3, Tid:8
t:4,i:0, Tid:8
T:4,i:1, Tid:8
T:4,i:2, Tid:8
T:4,i:3, Tid:8
t:5,i:0, Tid:8
T:5,i:1, Tid:8
T:5,i:2, Tid:8
T:5,i:3, Tid:8
t:6,i:0, Tid:8
T:6,i:1, Tid:8
T:6,i:2, Tid:8
T:6,i:3, Tid:8
t:7,i:0, Tid:8
T:7,i:1, Tid:8
T:7,i:2, Tid:8
T:7,i:3, Tid:8
t:8,i:0, Tid:8
T:8,i:1, Tid:8
T:8,i:2, Tid:8
T:8,i:3, Tid:8
t:9,i:0, Tid:8
T:9,i:1, Tid:8
T:9,i:2, Tid:8
T:9,i:3, Tid:8
Java Concurrency Programming (6) Thread Cheng control of the number of threads