设为首页 加入收藏

TOP

Android 自定义线程池的实战(一)
2017-10-13 10:47:23 】 浏览:1973
Tags:Android 定义 线程 池的 实战

前言:在上一篇文章中我们讲到了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);
}

  

2、ExecutorService 

     ExecutorService 是一个接口,继承 Executor ,除了有execute( Runnable command) 方法外,还拓展其他的方法:    

public interface ExecutorService extends Executor {

}
  • void shutdown();
  • List<Runnable> shutdownNow();
  • boolean isShutdown();
  • boolean isTerminated();
  • boolean awaitTermination(long timeout, TimeUnit unit)
    throws InterruptedException;
  • <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)
    throws InterruptedException;
  • <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
    long timeout, TimeUnit unit)
    throws InterruptedException;
  • <T> T invokeAny(Collection<? extends Callable<T>> tasks)
    throws InterruptedException, ExecutionException;
  • <T> T invokeAny(Collection<? extends Callable<T>> tasks,
    long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException;

   2.1 execute(Runnable)

        接收一个 java.lang.Runnable 对象作为参数,并且以异步的方式执行它。如下是一个使用 ExecutorService 执行 Runnable 的例子

package com.app;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorTest {
	
	public static void main(String[] args) {
		
		//创建一个线程数固定大小为10的线程池
		ExecutorService executorService = Executors.newFixedThreadPool( 10 ) ;
		
		//执行一个任务  该任务是 new Runnable() 对象
		executorService.execute( new Runnable() {
			
			@Override
			public void run() {
              Log.d( Thread.currentThread().getName() );				
			}
		});
		
		//关闭线程池
		executorService.shutdown();

	}
}

  结果:

pool-1-thread-1

     使用这种方式没有办法获取执行 Runnable 之后的结果,如果你希望获取运行之后的返回值,就必须使用 接收 Callable 参数的 execute() 方法,后者将会在下文中提到。

 

  &n

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android Handler机制(三)----Loop.. 下一篇【腾讯Bugly干货分享】Android进..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目