【多线程】Executor 和 ExecutorService 使用小结

本文详细介绍了Java中的Executor和ExecutorService接口,强调了使用ExecutorService而非直接new Thread的原因,包括资源管理和功能扩展等优势。文章通过实例演示了ExecutorService提供的四种线程池类型:newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool和newSingleThreadExecutor的工作原理和应用场景,帮助读者深入理解线程池的使用技巧。
摘要由CSDN通过智能技术生成

我们之前创建一个线程的时候,就是直到new一个Thread创建一个线程,但是在jdk1.5之后,Java为我们提供了一个Java.util.concurrent包,这个包下有Executor接口,这就为我们提供了线程池的方法去开启多个线程,那么说到这里我们是不是会问:为什么要用Executor呢?或者new的方式有什么缺点呢?

1)每次我们new Thread都会创建一个对象,不能被重用,而且对象的创建和销毁也是消耗资源的;

2)new Thread我们没法去设置定时、线程中断等功能;

3)new Thread缺乏统一的管理,我们new多了的时候,就会出现线程之间的竞争,或者占用过多的cpu资源,甚至可能导致死机;

那么反过来Executor肯定能解决这些问题,所以使用线程池更加有利;那么我们来看一下Executor:

查看源码我们可以看到Executor接口其实很简单,就一个方法:

public interface Executor {
 
    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

而ExecutorService接口继承了Executor接口,是Executor的扩展子接口;

这算他们之间的第一个区别,

而第二个区别是,Executor中的execute接口的是实现Runable接口的对象;而ExecutorService中的submit接收的实现Runable接口的对象或者callable接口的对象;

第三个区别是:Executor中的execute方法没有返回值,而submit方法有future返回值;

第四个区别是:ExecutorService还提供了控制线程池的方法;

这里说一下注意点:那就是ExecutorService的submit方法获取返回值get获取数据的时候,会导致get所在的线程发生堵塞,直到你返回值的线程执行完后,get线程才获取最终的结果;

ExecutorService为我们提供了四种线程池:

1)newCachedThreadPool线程池;

 public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值