newCachedThreadPool | 建立可以快取的Thread,每个Thread预设可idle 60秒 |
newFixedThreadPool | 包括固定数量的Thread |
newSingleThreadExecutor | 只有一个Thread,循序的执行指定给它的每个任务 |
newScheduledThreadPool | 可排程的Thread |
newSingleThreadScheduledExecutor | 单一可排程的Thread |
package onlyfun.caterpillar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorDemo {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
for(int i = 0; i < 10; i++) {
final int count = i;
service.submit(new Runnable() {
public void run() {
System.out.println(count);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
service.shutdown(); // 最后记得关闭Thread pool
}
}