关于Android的asynctask-threads-limits问题:asynctask开启的线程是否有极限
2015-10-18 16:05
501 查看
http://stackoverflow.com/questions/9654148/android-asynctask-threads-limits
antonyt:
All AsyncTasks are controlled internally by a shared (static) ThreadPoolExecutor and aLinkedBlockingQueue. When you call
The 'when am I ready?' behaviour of a
Before Android 1.6, the core pool size was 1 and the maximum pool size was 10. Since Android 1.6, the core pore size is 5, and the maximum pool size is 128. The size of the queue is 10 in both cases. The keep-alive timeout was 10 seconds before 2.3, and 1 second since then.
With all of this in mind, it now becomes clear why the
For completeness, if you repeated your exercise with more than 6 tasks (e.g. 30), you will see that more than 6 will enter
Android会开启一个线程池专门管理子线程启动问题,当线程池内线程数小于最大值时会启动线程,当线程池满时,当前线程未结束其他线程则要等待。
antonyt:
All AsyncTasks are controlled internally by a shared (static) ThreadPoolExecutor and aLinkedBlockingQueue. When you call
executeon an AsyncTask, the
ThreadPoolExecutorwill execute it when it is ready some time in the future.
The 'when am I ready?' behaviour of a
ThreadPoolExecutoris controlled by two parameters, the core pool size and the maximum pool size. If there are less than core pool size threads currently active and a new job comes in, the executor will create a new thread and execute it immediately. If there are at least core pool size threads running, it will try to queue the job and wait until there is an idle thread available (i.e. until another job is completed). If it is not possible to queue the job (the queue can have a max capacity), it will create a new thread (upto maximum pool size threads) for the jobs to run in. Non-core idle threads can eventually be decommissioned according to a keep-alive timeout parameter.
Before Android 1.6, the core pool size was 1 and the maximum pool size was 10. Since Android 1.6, the core pore size is 5, and the maximum pool size is 128. The size of the queue is 10 in both cases. The keep-alive timeout was 10 seconds before 2.3, and 1 second since then.
With all of this in mind, it now becomes clear why the
AsyncTaskwill only appear to execute 5/6 of your tasks. The 6th task is being queued up until one of the other tasks complete. This is a very good reason why you should not use AsyncTasks for long-running operations - it will prevent other AsyncTasks from ever running.
For completeness, if you repeated your exercise with more than 6 tasks (e.g. 30), you will see that more than 6 will enter
doInBackgroundas the queue will become full and the executor is pushed to create more worker threads. If you kept with the long-running task, you should see that 20/30 become active, with 10 still in the queue.
Android会开启一个线程池专门管理子线程启动问题,当线程池内线程数小于最大值时会启动线程,当线程池满时,当前线程未结束其他线程则要等待。
相关文章推荐
- Android通知之自定义通知、响应通知上自定义按钮和更新通知的实现
- Android下拉刷新上拉加载控件,对所有View通用!
- Android-----第三方 ImageLoader 的简单配置和使用
- 使用git查阅Android M中Doze相关代码
- Android应用程序资源文件的编译和打包原理
- Android Studio使用疑难问题汇总
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- Android 弹出软键盘android:windowSoftInputMode设置总结
- Android 学习笔记之Volley开源框架解析(二)
- Android greenDao的简单配置和使用
- Android通知之通知栏点击通知后返回正在运行的程序,而不是一个新Activity
- android stidio so动态库找不到问题
- Android高效加载大图片,防止OOM
- Android6.0新特性??
- Binder
- Android Java连接MySQL数据库
- Diving Into Android 'M' Doze
- Android应用程序资源查找过程分析
- Android 实现子View的状态跟随父容器的状态
- android 37 线程通信Looper