ExecutorService test 04

package t1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThread16 {

public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
System.out.println(Runtime.getRuntime().availableProcessors());// 可用线程数,加上主线程共4个
for (int i = 0; i < 10; i++) {
final int index = i;
es.execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " " + index);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}

result:

The number of fixed thread, the task is executed, the thread pool thread to recover, does not terminate the program

 

Guess you like

Origin www.cnblogs.com/dengw125792/p/12605810.html