线程池 Executor 一篇简单笔记就懂

线程池Executor

目录

1、线程池简介

2、线程池的好处

3、线程池执行任务的规则

4、线程池的分类

1)FixedThreadPool类型

2)CachedThreadPool类型

3)ScheduleThreadPool类型

4)SingleThreadExecutor类型


1、线程池简介

线程池是在java开发中很重要的一个概念,Android中的线程池和和java是保持一致的,并无什么区别。线程池是一个抽象的概念,在java中是一个接口类,用Executor表示, 具体实现类为ThreadPoolExecutor,它们位于java.util.concurrent包下面,这个接口类很短,而且接口声明就只有一个,并且注释中说明了一些情况怎么使用,

package java.util.concurrent;
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);
}

上面省略了注释,可以看注释,很管用,上面有模版,教我们怎么创建一个自己线程池,其中有段是这么说的:许多线程池的实现类对将要执行的任务的方式和时间做了限制,下面展示了串型线程池在执行任务的时候,从一个任务到下一个任务的过程。

下面代码来至注释中的举例,说明了一个从任务如何进入到下一个任务的串行执行过程:

package demo.xx.patten.xx;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Executor;

public class SerialExecutor implements Executor {
    final Queue<Runnable> tasks = new ArrayDeque<>();
    final Executor executor;
    Runnable active;

    public SerialExecutor(Executor executor) {
        this.exec
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值