name

珍惜眼前人

导航

线程池

导入的包都是concurrent并发包

1、 中可以随意的开辟线程池接口 

// ExecutorService是一个接口, 不能直接构建对象
// 使用工厂模式的方法构建出线程池对象
// 此对象就是一个线程池管理对象,封装了对线程的管理策略,
// 帮助我们实现线程的重用.

ExecutorService pool = Executors.newCachedThreadPool();
pool.execute(new Demo3A());

2、创建固定数量的线程池 

ExecutorService pool = Executors.newFixedThreadPool(3);

for (int i = 0; i < 15; i++) {
pool.execute(new Demo4A());
}

3、

/ 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。
/**
* .newScheduledThreadPool(int corePoolSize)池中所保存的线程数,包含空闲的线程
*/
ScheduledExecutorService timer = Executors.newScheduledThreadPool(3);

/**
*参数:
command - 要执行的任务
initialDelay - 首次执行的延迟时间
period - 连续执行之间的周期
unit - initialDelay 和 period 参数的时间单位
*/
//创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期
 timer.scheduleAtFixedRate(new Demo5A(), 0, 1, TimeUnit.SECONDS);

 

posted on 2015-10-21 15:25  珍惜眼前人  阅读(84)  评论(0编辑  收藏  举报