您的位置:首页 > 编程语言 > Java开发

AsyncTask源码分析之Java篇

2016-03-29 14:57 579 查看
解析AsyncTask源码之前,列举一下AsyncTask源码涉及到关于Java中线程到知识点:

知识点清单

1、Thread类

2、Runnable接口

3、Callable接口

4、Future接口

5、RunnableFuture接口

6、FutureTask类

7、ArrayQueue

8、LinkedBlockingQueue

首先讲述的是Thread这个类,最普通的用法莫过于

new Thread()


这个就是一个新的线程,然后用start()方法启动。

Runnable接口就是一个普通的接口,回调Runnbale接口的方法就是用run方法。

上述两种基本上会JAVA的人都知道,所以不给大家详细解释,第三种就是Callbale接口,大家请看代码:

* @see Executor
* @since 1.5
* @author Doug Lea
* @param <V> the result type of method {@code call}
*/
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}


callable接口其实是与Runnable相类似的一个接口,差别的地方在于callable里面的方法是call,并且可以有返回的结果,否则就抛出异常,而Runable接口仅仅有一个run方法。

Future接口

public interface Future<V> {
//取消当前的任务,标志位表示是否马上取消当前的任务
boolean cancel(boolean mayInterruptIfRunning);
//任务是否已经取消了
boolean isCancelled();
//任务是否已经完成了
boolean isDone();
//获取结果,如果任务未完成,则等待,知道完全,因此该函数会阻塞
V get() throws InterruptedException, ExecutionException;
//获取结果,如果任务未完成,则等待,知道超时或者返回结果,该函数会阻塞
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}


RunnableFuture接口

public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}


这个接口其实就是即实现了Runnable接口,又实现了Future接口。

FutureTask类

public class FutureTask<V> implements RunnableFuture<V>


FutureTask这个类实际上是实现了RunableFuture这个接口的,前面的知识其实主要就是为了这里做铺垫,这里主要为大家讲述一下FutureTask几个常用的方法

isDone() 是否已经执行完毕

cancle(boolean mayInterruptIfRunning) 取消当前任务,里面的布尔值参数决定是否立刻取消当前的任务,无论该任务是否已经完成

isCancelled() 当前任务是否已经取消 成功则返回true

V get() 获得任务完全的结果,如果没有结果该方法会一直阻塞在那边

V get(long timeout, TimeUnit unit) 中间两个参数一个设置超时时间,一个设时间单位如分秒这些单位。该方法会一直阻塞,直到返回结果或者超时

FutureTask 有两个构造方法

public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
sync = new Sync(callable);
}


public FutureTask(Runnable runnable, V result) {
sync = new Sync(Executors.callable(runnable, result));
}


第一个构造方法是在里面放入一个callable接口,第二个构造方法是放入一个runnbale接口以及完成时需要返回的结构,那我们来看一下
Executors.callable(runnable, result)


这个里面是什么

public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);
}


static final class RunnableAdapter<T> implements Callable<T> {
final Runnable task;
final T result;
RunnableAdapter(Runnable task, T result) {
this.task = task;
this.result = result;
}
public T call() {
task.run();
return result;
}
}


大家请看这两段代码,这是Executros里面callable方法的实现,其实在这里面用了一个适配器模式,在RunnableAdapter这个适配器中实现了Callable接口,在这个接口的call方法里面,调用了Runnable接口的run方法,并且返回了开始传入的结果。

ArrayQueue

这个平常大家用的相对较少,其实这是一个数组队列,队列的特性就是先进先出。这个数组队列add(),remover(),offer(),pull()这几种常用的方法,其实add()与offer()是同一个方法,只不过offer封装了一个返回值remove()与pull()也是一样的

LinkedBlockingQueue

BlockingQueue线程阻塞式队列

这种队列除了有先进先出的特性以外,还有一个特征,就是当生产者线程试图向BlockingQueue中放入元素,如果该队列已满,则线程被阻塞,当消费者试图从BlockingQueue取出元素时,如果该队列为空,则该线程被阻塞。LinkedBlockingQueue是基于链表实现的BlockingQueue队列。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: