首页 > 经验记录 > 线程池的四种基本创建方式

线程池的四种基本创建方式

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
public static void main(String[] args) {
//创建固定长度的线程池
ExecutorService pool1 = Executors.newFixedThreadPool(3);
for(int i = 1;i <= 4;i++){
pool1.execute(new MyThread());
}
pool1.shutdown();
//创建可变长度的线程池
ExecutorService pool2 = Executors.newCachedThreadPool();
for(int i = 1;i <= 4;i++){
pool2.execute(new MyThread());
}
//创建单个线程的线程池
ExecutorService pool3 = Executors.newSingleThreadExecutor();
for(int i = 1;i <= 4;i++){
pool3.execute(new MyThread());
}
//创建周期性执行某个任务的线程池
ScheduledExecutorService pool4 = Executors.newScheduledThreadPool(1);
pool4.scheduleAtFixedRate(new Demo(), 2, 3, TimeUnit.SECONDS);
}
}

           


CAPTCHAis initialing...

1 COMMENT

  1. 付超麒2018-05-04 20:47

    不错还挺详细的

EA PLAYER &

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

      00:00/00:00