线程池中Executor、ExecuteService、Executors 的区别

Executor、ExecuteService都是接口,ExecuteService继承于Executor,

Executor:接口
接口Executor里面只有一个execute方法: 
void execute(Runnable command)  

ExecutorService:接口
接口ExecutorService继承于Executor
public interface ExecutorService extends Executor {  
    void shutdown();  
    List<Runnable> shutdownNow();  
    boolean isShutdown();  
    boolean isTerminated();  
    boolean awaitTermination(long timeout, TimeUnit unit)  
    <T> Future<T> submit(Callable<T> task);  
    <T> Future<T> submit(Runnable task, T result);  
    Future<?> submit(Runnable task);  
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)  
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)  
    <T> T invokeAny(Collection<? extends Callable<T>> tasks)  
    <T> T invokeAny(Collection<? extends Callable<T>> tasks,  
                    long timeout, TimeUnit unit)  
}  

Executors :类
直接援引JDK文档的说明来说一下这个类的作用
Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes defined in this package. 
实例:
public class CacheThreadPool {  
    public static void main(String[] args) {  
        ExecutorService exec=Executors.newCachedThreadPool();  
        for(int i=0;i<5;i++)  
            exec.execute(new LiftOff());  
        exec.shutdown();//并不是终止线程的运行,而是禁止在这个Executor中添加新的任务  
    }  
} 
上面例子用到了Executors类的newCachedThreadPool()方法。看一下这个方法:
public static ExecutorService newCachedThreadPool() {  
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,  
                                      60L, TimeUnit.SECONDS,  
                                      new SynchronousQueue<Runnable>());  
    }  
 在源码中我们可以知道两点
1、newCachedThreadPool()方法返回类型是ExecutorService;ExecutorService中有一个execute方法,这个方法的参数是Runnable类型。也就是说,将一个实现了Runnable类型的类的实例作为参数传入execute方法并执行,那么线程就相应的执行了。 
2、方法newCachedThreadPool()返回值实际是另一个类ThreadPoolExecutor的实例。

引用文档:
http://www.cnblogs.com/yezhenhan/archive/2012/01/07/2315645.html
http://cuisuqiang.iteye.com/blog/2019372

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值