你的位置:首页 > 软件开发 > 操作系统 > Android 自定义线程池的实战

Android 自定义线程池的实战

发布时间:2016-08-12 11:00:12
前言:在上一篇文章中我们讲到了AsyncTask的基本使用、AsyncTask的封装、AsyncTask 的串行/并行线程队列、自定义线程池、线程池的快速创建方式。对线程池不了解的同学可以先看 Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池 ...

前言:在上一篇文章中我们讲到了AsyncTask的基本使用、AsyncTask的封装、AsyncTask 的串行/并行线程队列、自定义线程池、线程池的快速创建方式。

对线程池不了解的同学可以先看 Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池 

 

-------------------------------------------------------------------------------------------------------

1、Executor 简介

     在Java 5之后,并发编程引入了一堆新的启动、调度和管理线程的API。Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.cocurrent 包下,通过该框架来控制线程的启动、执行和关闭,可以简化并发编程的操作。因此,在Java 5之后,通过Executor来启动线程比使用Thread的start方法更好,除了更易管理,效率更好(用线程池实现,节约开销)外,还有关键的一点:有助于避免this逃逸问题——如果我们在构造器中启动一个线程,因为另一个任务可能会在构造器结束之前开始执行,此时可能会访问到初始化了一半的对象用Executor在构造器中。

   Executor框架包括:线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。

   在java代码中 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);}

原标题:Android 自定义线程池的实战

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录