Android之AsyncTask两种线程池分析和总结
【摘要】 Android AsyncTask两种线程池分析和总结
(一) 前言
在android AsyncTask里面有两种线程池供我们调用
1. THREAD_POOL_EXECUTOR, 异步线程池
2. SERIAL_EXECUTOR,同步线程池
正如上面名称描述的那样,一个是异步线程池,多...
(二) THREAD_POOL_EXECUTOR用法举例
-
private static int produceTaskMaxNumber = 500;
-
02
-
public void dotask(){
-
03
-
for (int i = 1; i <= produceTaskMaxNumber; i++){
-
04
-
// 产生一个任务,并将其加入到线程池
-
05
-
String task = "task@ " + i;
-
06
-
Log.d("Sandy", "put " + task);
-
07
-
MyAsyncTask asynct = new MyAsyncTask(task);
-
08
-
asynct.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 0);
-
09
-
}
-
10
-
}
-
11
-
-
12
-
static class MyAsyncTask extends AsyncTask<Integer, Integer, Integer>{
-
13
-
private static int consumeTaskSleepTime = 2000;
-
14
-
// 保存任务所需要的数据
-
15
-
private Object threadPoolTaskData;
-
16
-
public MyAsyncTask(String s){
-
17
-
threadPoolTaskData = s;
-
18
-
}
-
19
-
<a href="http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
-
20
-
protected Integer doInBackground(Integer... arg0) {
-
21
-
Log.d("Sandy", "start .." + threadPoolTaskData
-
22
-
+ " thread id: " + Thread.currentThread().getId()
-
23
-
+ " thread name: " + Thread.currentThread().getName());
-
24
-
try {
-
25
-
// //便于观察,等待一段时间
-
26
-
Thread.sleep(consumeTaskSleepTime);
-
27
-
}
-
28
-
catch (Exception e) {
-
29
-
Log.d("Sandy", "", e);
-
30
-
}
-
31
-
threadPoolTaskData = null;
-
32
-
return 0;
-
33
-
}
-
34
-
}
-
MyAsyncTask asynct = new MyAsyncTask(task);
-
2
-
asynct.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 0);
就是因为我们尝试添加500个task到AsyncTask.THREAD_POOL_EXECUTOR线程池中,但是它的核心线程是5,队列容量是128,最大线程数是9。
所以,抛出了这个异常。
那么,接下来的话,我们会去分析这个异常怎么出来的。
(三) THREAD_POOL_EXECUTOR代码分析
从AsyncTask.THREAD_POOL_EXECUTOR的定义开始分析
1. 代码路径
frameworks\base\core\java\android\os\AsyncTask.java
代码:
-
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
-
02
-
private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
-
03
-
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
-
04
-
private static final int KEEP_ALIVE = 1;
-
05
-
-
06
-
....
-
07
-
/**
-
08
-
* An {@link Executor} that can be used to execute tasks in parallel.
-
09
-
*/
-
10
-
public static final Executor THREAD_POOL_EXECUTOR
-
11
-
= new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
-
12
-
TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
-
public ThreadPoolExecutor(int corePoolSize,
-
02
-
int maximumPoolSize,
-
03
-
long keepAliveTime,
-
04
-
TimeUnit unit,
-
05
-
BlockingQueue<Runnable> workQueue,
-
06
-
ThreadFactory threadFactory,
-
07
-
RejectedExecutionHandler handler) {
-
08
-
if (corePoolSize < 0 ||
-
09
-
maximumPoolSize <= 0 ||
-
10
-
maximumPoolSize < corePoolSize ||
-
11
-
keepAliveTime < 0)
-
12
-
throw new IllegalArgumentException();
-
13
-
if (workQueue == null || threadFactory == null || handler == null)
-
14
-
throw new NullPointerException();
-
15
-
this.corePoolSize = corePoolSize;
-
16
-
this.maximumPoolSize = maximumPoolSize;
-
17
-
this.workQueue = workQueue;
-
18
-
this.keepAliveTime = unit.toNanos(keepAliveTime);
-
19
-
this.threadFactory = threadFactory;
-
20
-
this.handler = handler;
-
21
-
}
-
22
-
-
23
-
/**
-
24
-
* The default rejected execution handler
-
25
-
*/
-
26
-
private static final RejectedExecutionHandler defaultHandler =
-
27
-
new AbortPolicy();
1 |
a. asynct.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, 0 ); |
2 |
b. asynct.execute( 0 ); |
01 |
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, |
02 |
Params... params) { |
03 |
... |
04 |
exec.execute(mFuture); |
05 |
.... |
06 |
} |
07 |
08 |
private static class SerialExecutor implements Executor { |
09 |
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>(); |
10 |
Runnable mActive; |
11 |
public synchronized void execute( final Runnable r) { |
12 |
mTasks.offer( new Runnable() { |
13 |
public void run() { |
14 |
try { |
15 |
r.run(); |
16 |
} finally { |
17 |
scheduleNext(); |
18 |
} |
19 |
} |
20 |
}); |
21 |
if (mActive == null ) { |
22 |
scheduleNext(); |
23 |
} |
24 |
} |
25 |
26 |
protected synchronized void scheduleNext() { |
27 |
if ((mActive = mTasks.poll()) != null ) { |
28 |
THREAD_POOL_EXECUTOR.execute(mActive); |
29 |
} |
30 |
} |
31 |
} |
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/50494611
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)