java线程池常用类

来源:互联网 发布:淘宝网的卖家中心在哪 编辑:程序博客网 时间:2024/06/05 08:44

1.概述:学习理解线程池常用类
2.生成相关:
a.Executor(接口),抽象出执行所提交的Runnable对象,与任务调度分离;
Executor 接口并没有严格地要求执行是异步,可以结合调度方式:

class SerialExecutor implements Executor {     final Queue<Runnable> tasks = new ArrayDeque<Runnable>();     final Executor executor;     Runnable active;     SerialExecutor(Executor executor) {         this.executor = executor;     }     public synchronized void execute(final Runnable r) {         tasks.offer(new Runnable() {             public void run() {                 try {                     r.run();                 } finally {                     scheduleNext();                 }             }         });         if (active == null) {             scheduleNext();         }     }     protected synchronized void scheduleNext() {         if ((active = tasks.poll()) != null) {             executor.execute(active);         }     } }

b.ExecutorService(接口),继承了Executor接口,进一步抽象出线程池服务程序的方法
c.Executors是个静态工厂类。它通过静态工厂方法返回ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 等类的对象。
线程池架构图:
这里写图片描述

原创粉丝点击