本人进行过模拟測试,发现AsyncTask并不适合多任务,以及长期的异步任务,由于每次仅仅能执行一个AsyncTask,假设执行多个其他任务将会等待
以下通过一个代码样例和日志打印得到证实。
以下扩展了2个AsyncTask,分别调用了2次,一共调用了四次。
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 4个异步一起运行,2个不同的实现对象
new MyTask().execute("http://www.baidu.com/img/bdlogo.png");
new MyTask2().execute("http://www.baidu.com/img/bdlogo.png");
new MyTask2().execute("http://www.baidu.com/img/bdlogo.png");
new MyTask().execute("http://www.baidu.com/img/bdlogo.png");
} public class MyTask2 extends AsyncTask<String, Integer, Bitmap> { public MyTask2() {
super(); } @Override
protected void onPostExecute(Bitmap result) {
Log.d("MainActivity", "onPostExecute");
updateImageView(result);
} @Override
protected void onPreExecute() {
Log.d("MainActivity", "onPreExecute");
} @Override
protected Bitmap doInBackground(String... args) {
Log.e("MainActivity", "MyTask2:" + THREAD_POOL_EXECUTOR); // static对象都一样,运行的线程池 Log.e("MainActivity", "MyTask2:" + SERIAL_EXECUTOR); // static对象都一样,运行任务的 String website = args[0];
HttpURLConnection conn = null;
InputStream ins = null;
try {
for (int i = 0; i < 10; i++) {
Thread.sleep((int) (Math.random() * 100 + 800)); <span style="font-family: Arial, Helvetica, sans-serif;">//有益休眠</span>
Log.d("MainActivity", "MyTask2:" + i + " " + Thread.currentThread().getName() + " id:"
+ Thread.currentThread().getId());
}
URL url = new URL(website);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
Log.d("MainActivity", "MyTask2:" + "get image is ok");
ins = conn.getInputStream();
return BitmapFactory.decodeStream(ins);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} } public class MyTask extends AsyncTask<String, Integer, Bitmap> { public MyTask() {
super(); } @Override
protected void onPostExecute(Bitmap result) {
Log.d("MainActivity", "onPostExecute");
updateImageView(result);
} @Override
protected void onPreExecute() {
Log.d("MainActivity", "onPreExecute");
} @Override
protected Bitmap doInBackground(String... args) {
Log.e("MainActivity", "MyTask:" + THREAD_POOL_EXECUTOR);
Log.e("MainActivity", "MyTask:" + SERIAL_EXECUTOR); for (int i = 0; i < 10; i++) {
try {
Thread.sleep((int) (Math.random() * 100 + 600));//有益休眠
} catch (InterruptedException e) { e.printStackTrace();
}
Log.d("MainActivity", "MyTask:" + i + " " + Thread.currentThread().getName() + " id:"
+ Thread.currentThread().getId());
}
String website = args[0];
HttpURLConnection conn = null;
InputStream ins = null;
try {
URL url = new URL(website);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
Log.d("MainActivity", "get image is ok");
ins = conn.getInputStream();
return BitmapFactory.decodeStream(ins);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} } boolean f = false; public void updateImageView(Bitmap result) {
if (result != null) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result); } } }
4个异步执行发现,第一个必须执行完才干执行下一个? 仅仅是我理解不正确,还是使用方法有问题,假设有朋友知道请指正。
打印日志例如以下:
12-17 05:24:57.665: D/MainActivity(25755): onPreExecute
12-17 05:24:57.665: D/dalvikvm(25755): create interp thread : stack size=32KB
12-17 05:24:57.665: D/dalvikvm(25755): create new thread
12-17 05:24:57.666: D/dalvikvm(25755): new thread created
12-17 05:24:57.666: D/dalvikvm(25755): update thread list
12-17 05:24:57.666: D/dalvikvm(25755): threadid=16: interp stack at 0x5e677000
12-17 05:24:57.666: D/dalvikvm(25755): threadid=16: created from interp
12-17 05:24:57.666: D/dalvikvm(25755): start new thread
12-17 05:24:57.666: D/dalvikvm(25755): threadid=16: notify debugger
12-17 05:24:57.666: D/dalvikvm(25755): threadid=16 (AsyncTask #5): calling run()
12-17 05:24:57.666: D/MainActivity(25755): onPreExecute
<span style="color:#ff0000;">12-17 05:24:57.666: E/MainActivity(25755): MyTask:java.util.concurrent.ThreadPoolExecutor@42258eb8[Running, pool size = 5, active threads = 1, queued tasks = 0, completed tasks = 4]
12-17 05:24:57.666: E/MainActivity(25755): MyTask:android.os.AsyncTask$SerialExecutor@42258fc8</span>
12-17 05:24:57.669: D/MainActivity(25755): onPreExecute
12-17 05:24:57.669: D/MainActivity(25755): onPreExecute
12-17 05:24:57.731: V/InputMethodManager(25755): onWindowFocus: null softInputMode=288 first=true flags=#1810100
12-17 05:24:57.731: V/InputMethodManager(25755): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{422f7c58 V.E..... R.....ID 0,0-1080,1920} ic=null tba=android.view.inputmethod.EditorInfo@422fe938 controlFlags=#104
12-17 05:24:57.733: V/InputMethodManager(25755): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@42301bb0 com.android.inputmethod.latin/.LatinIME #609}
12-17 05:24:58.281: D/MainActivity(25755): MyTask:0 AsyncTask #5 id:4784
12-17 05:24:58.966: D/MainActivity(25755): MyTask:1 AsyncTask #5 id:4784
12-17 05:24:59.643: D/MainActivity(25755): MyTask:2 AsyncTask #5 id:4784
12-17 05:25:00.276: D/MainActivity(25755): MyTask:3 AsyncTask #5 id:4784
12-17 05:25:00.946: D/MainActivity(25755): MyTask:4 AsyncTask #5 id:4784
12-17 05:25:01.589: D/MainActivity(25755): MyTask:5 AsyncTask #5 id:4784
12-17 05:25:02.224: D/MainActivity(25755): MyTask:6 AsyncTask #5 id:4784
12-17 05:25:02.842: D/MainActivity(25755): MyTask:7 AsyncTask #5 id:4784
12-17 05:25:03.464: D/MainActivity(25755): MyTask:8 AsyncTask #5 id:4784
12-17 05:25:04.120: D/MainActivity(25755): MyTask:9 AsyncTask #5 id:4784
12-17 05:25:04.123: I/System.out(25755): [CDS]rx timeout:0
12-17 05:25:04.125: I/System.out(25755): [CDS]close[48692]
12-17 05:25:04.126: I/System.out(25755): close [socket][/0.0.0.0:48692]
12-17 05:25:04.430: D/libc-netbsd(25755): getaddrinfo: www.baidu.com get result from proxy >>
12-17 05:25:04.430: I/System.out(25755): propertyValue:true
12-17 05:25:04.431: I/System.out(25755): [socket][1] connection www.baidu.com/180.97.33.107:80;LocalPort=38503(5000)
12-17 05:25:04.431: I/System.out(25755): [CDS]connect[www.baidu.com/180.97.33.107:80] tm:5
12-17 05:25:04.432: D/Posix(25755): [Posix_connect Debug]Process com.example.asynctaskdemo :80
12-17 05:25:04.547: I/System.out(25755): [socket][/192.168.2.122:38503] connected
12-17 05:25:04.547: I/System.out(25755): [CDS]rx timeout:0
12-17 05:25:04.613: D/MainActivity(25755): get image is ok
12-17 05:25:04.643: D/dalvikvm(25755): GC_FOR_ALLOC freed 677K (925), 32% free 3736K/5484K, paused 23ms, total 28ms
12-17 05:25:04.646: D/skia(25755): Flag is not 10
12-17 05:25:04.647: D/MainActivity(25755): onPostExecute
<span style="color:#ff0000;">12-17 05:25:04.648: E/MainActivity(25755): MyTask2:java.util.concurrent.ThreadPoolExecutor@42258eb8[Running, pool size = 5, active threads = 1, queued tasks = 0, completed tasks = 5]
12-17 05:25:04.648: E/MainActivity(25755): MyTask2:android.os.AsyncTask$SerialExecutor@42258fc8</span>
12-17 05:25:04.668: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.14, dur:6910.24, max:6910.24, min:6910.24
12-17 05:25:04.668: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:6910.24
12-17 05:25:05.485: D/MainActivity(25755): MyTask2:0 AsyncTask #5 id:4784
12-17 05:25:06.323: D/MainActivity(25755): MyTask2:1 AsyncTask #5 id:4784
12-17 05:25:07.217: D/MainActivity(25755): MyTask2:2 AsyncTask #5 id:4784
12-17 05:25:08.107: D/MainActivity(25755): MyTask2:3 AsyncTask #5 id:4784
12-17 05:25:09.002: D/MainActivity(25755): MyTask2:4 AsyncTask #5 id:4784
12-17 05:25:09.881: D/MainActivity(25755): MyTask2:5 AsyncTask #5 id:4784
12-17 05:25:10.771: D/MainActivity(25755): MyTask2:6 AsyncTask #5 id:4784
12-17 05:25:11.579: D/MainActivity(25755): MyTask2:7 AsyncTask #5 id:4784
12-17 05:25:12.445: D/MainActivity(25755): MyTask2:8 AsyncTask #5 id:4784
12-17 05:25:13.299: D/MainActivity(25755): MyTask2:9 AsyncTask #5 id:4784
12-17 05:25:13.300: I/System.out(25755): [CDS]rx timeout:0
12-17 05:25:13.586: D/MainActivity(25755): MyTask2:get image is ok
12-17 05:25:13.610: D/dalvikvm(25755): GC_FOR_ALLOC freed 688K (1136), 33% free 3689K/5484K, paused 21ms, total 21ms
12-17 05:25:13.613: D/skia(25755): Flag is not 10
12-17 05:25:13.613: D/MainActivity(25755): onPostExecute
<span style="color:#ff6666;">12-17 05:25:13.615: E/MainActivity(25755): MyTask2:java.util.concurrent.ThreadPoolExecutor@42258eb8[Running, pool size = 5, active threads = 1, queued tasks = 0, completed tasks = 6]
12-17 05:25:13.615: E/MainActivity(25755): MyTask2:android.os.AsyncTask$SerialExecutor@42258fc8</span>
12-17 05:25:13.630: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.11, dur:8961.83, max:8961.83, min:8961.83
12-17 05:25:13.630: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:8961.83
12-17 05:25:14.455: D/MainActivity(25755): MyTask2:0 AsyncTask #5 id:4784
12-17 05:25:15.304: D/MainActivity(25755): MyTask2:1 AsyncTask #5 id:4784
12-17 05:25:16.104: D/MainActivity(25755): MyTask2:2 AsyncTask #5 id:4784
12-17 05:25:17.008: D/MainActivity(25755): MyTask2:3 AsyncTask #5 id:4784
12-17 05:25:17.887: D/MainActivity(25755): MyTask2:4 AsyncTask #5 id:4784
12-17 05:25:18.737: D/MainActivity(25755): MyTask2:5 AsyncTask #5 id:4784
12-17 05:25:19.584: D/MainActivity(25755): MyTask2:6 AsyncTask #5 id:4784
12-17 05:25:20.399: D/MainActivity(25755): MyTask2:7 AsyncTask #5 id:4784
12-17 05:25:21.281: D/MainActivity(25755): MyTask2:8 AsyncTask #5 id:4784
12-17 05:25:22.130: D/MainActivity(25755): MyTask2:9 AsyncTask #5 id:4784
12-17 05:25:22.132: I/System.out(25755): [CDS]rx timeout:0
12-17 05:25:22.178: D/MainActivity(25755): MyTask2:get image is ok
12-17 05:25:22.202: D/dalvikvm(25755): GC_FOR_ALLOC freed 641K (300), 33% free 3689K/5484K, paused 21ms, total 22ms
12-17 05:25:22.208: D/skia(25755): Flag is not 10
12-17 05:25:22.209: D/MainActivity(25755): onPostExecute
<span style="color:#ff0000;">12-17 05:25:22.211: E/MainActivity(25755): MyTask:java.util.concurrent.ThreadPoolExecutor@42258eb8[Running, pool size = 5, active threads = 1, queued tasks = 0, completed tasks = 7]
12-17 05:25:22.211: E/MainActivity(25755): MyTask:android.os.AsyncTask$SerialExecutor@42258fc8</span>
12-17 05:25:22.219: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.12, dur:8589.36, max:8589.36, min:8589.36
12-17 05:25:22.219: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:8589.36
12-17 05:25:22.839: D/MainActivity(25755): MyTask:0 AsyncTask #5 id:4784
12-17 05:25:23.519: D/MainActivity(25755): MyTask:1 AsyncTask #5 id:4784
12-17 05:25:24.176: D/MainActivity(25755): MyTask:2 AsyncTask #5 id:4784
12-17 05:25:24.786: D/MainActivity(25755): MyTask:3 AsyncTask #5 id:4784
12-17 05:25:25.440: D/MainActivity(25755): MyTask:4 AsyncTask #5 id:4784
12-17 05:25:26.134: D/MainActivity(25755): MyTask:5 AsyncTask #5 id:4784
12-17 05:25:26.740: D/MainActivity(25755): MyTask:6 AsyncTask #5 id:4784
12-17 05:25:27.367: D/MainActivity(25755): MyTask:7 AsyncTask #5 id:4784
12-17 05:25:27.988: D/MainActivity(25755): MyTask:8 AsyncTask #5 id:4784
12-17 05:25:28.617: D/MainActivity(25755): MyTask:9 AsyncTask #5 id:4784
12-17 05:25:28.618: I/System.out(25755): [CDS]rx timeout:0
12-17 05:25:28.658: D/MainActivity(25755): get image is ok
12-17 05:25:28.681: D/dalvikvm(25755): GC_FOR_ALLOC freed 641K (299), 33% free 3689K/5484K, paused 20ms, total 21ms
12-17 05:25:28.683: D/skia(25755): Flag is not 10
12-17 05:25:28.684: D/MainActivity(25755): onPostExecute
12-17 05:25:28.694: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.15, dur:6474.71, max:6474.71, min:6474.71
12-17 05:25:28.694: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:6474.71
发现ThreadPool每次仅仅有1个线程在执行,一个时刻仅仅能执行一次。上面执行的情况是线程被反复利用了,
手机上执行线程NAME和ID都是一样,在模拟器上执行ID和NAME即便是不一样 也是顺序执行的,并没有同一时候执行多个任务的情况发现。
个人经过測试发现,AsyncTask 仅仅能用一次,假设实例化多个AsyncTask 并不能提高执行效率,不能利用多任务的情况。不知道是不是这样,google设计这个AsyncTask的目的仅仅是用来执行一次异步任务,和UI更新? 如有朋友知道详细理由请指出,,谢谢。
AsyncTask測试多任务的更多相关文章
-
DBCP,C3P0,Tomcat_JDBC 性能及稳定性測试
原创文章,转载请指明出处:http://aub.iteye.com/blog/1404219, 尊重他人即尊重自己 DBCP,C3P0,Tomcat_JDBC 性能及稳定性測试 1.測试环境: 硬件环 ...
-
Android自己主动化測试解决方式
如今,已经有大量的Android自己主动化測试架构或工具可供我们使用,当中包含:Activity Instrumentation, MonkeyRunner, Robotium, 以及Robolect ...
-
java 搭建webservice服务+testclient測试
整理别人的日志: 一.什么是webservice 一种构建应用程序的普遍模型,能够在不论什么支持网络通信的操作系统中执行.一种新的web应用程序分支,能够公布.定位通过web调用.它是一个应用组件,为 ...
-
玩转Bash脚本:test測试语句
总第1篇test就是測试的意思,经常使用在流程控制语句中作为条件.以下做一下介绍. 关于真值 与其它语言不同,Bash(包含其它Shell)中,是用0表示真,非0表示假的.之所以用0表示成功,而不是1 ...
-
MYSQL BLOB 字段大小以及个数的限制測试。
測试结论 mysql版本号 5.1 表类型: innodb, row_format=compact (这是默认的行格式) 插入超过10个blob, blob的数据量非常小(<76 ...
-
python第三方库系列之十九--python測试使用的mock库
一.为什么须要mock 在写unittest的时候,假设系统中有非常多外部依赖,我们不须要也不希望把全部的部件都执行一遍.比方,要验证分享到微博的功能,假设每次測试的时候都要真实地把接 ...
-
Cocos2d-x 3.0- 在Visual Studio 2012中执行測试项目
Cocos2d-x - 怎样在Win32执行cpp-tests 2014年4月30日 星期三 小雨 微凉 稍显疲惫 注:本篇文章来自Cocos2d-x官网,小巫仅仅是粗略翻译眼下最新版本号的,教大家怎 ...
-
Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完毕測试代码)
MD5和RSA是网络传输中最经常使用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,仅仅能加密而不能解密. ...
-
利用Continuous Testing实现Eclipse环境自己主动单元測试
当你Eclipse环境中改动项目中的某个方法时,你可能因为各种原因没有执行单元測试,结果代码提交,悲剧就可能随之而来. 所幸infinitest(http://infinitest.github.io ...
随机推荐
-
【JAVA并发编程实战】10、并发程序的测试
1.产生随机数 package cn.study.concurrency.ch12; public class Util { public static int xorShift(int y) { / ...
-
webform组合查询和分页
1.组合查询(1)数据访问类 //参数1:SQL语句 参数2:哈希表public List<Users> chas(string s,Hashtable has) { List<Us ...
-
使用git ftp发布我个人的hexo博客内容
自己虚拟主机中的博客是由hexo3 + next主题,因为我想将 hexo 编译生成的文件可以通过ftp命令发布到ftp服务器上面. 发布使用的工具是git-ftp: 按照Use Jenkins an ...
-
AIDL 发生异常的原因 Android java.lang.SecurityException: Binder invocation to an incorrect interface
我建立了两个project.一个是activity 的 ,一个是service 的. 在进行两个project通信时,应该有以下几点注意: 1.在activity project中引入service ...
-
使用 gulp-file-include 构建前端静态页面
前言 虽然现在单页面很流行,但是在 PC 端多页面还是常态,所以构建静态页面的工具还有用武之地.最近也看到了一些询问如何 include HTML 文件的问题. 很多时候我们在写静态页面的时候也希望能 ...
-
Java冒泡排序法升级版
/* * 冒泡排序之升级版,可比较整型数组.小数型数组 * * */ public static <T extends Comparable<T>> void Bubb ...
-
安卓7.0拍照遇到 Uri暴露错误
最近,项目又做到,调用摄像头拍照获取图片这个功能. 用以前的代码直接用,发现在Android7.0上使用时会出现问题. Android6.0之后,动态申请权限已成常态. 调用摄像头拍照获取图片这个功能 ...
-
动态添加一列到DataTable的第一列
dt.Columns.Add("ROWNUM", typeof(Int64));dt.Columns["ROWNUM"].SetOrdinal(0);
-
jstl 遍历数据
1 导入 jstl 的 jar 包 2. 页面中添加 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/ ...
-
UncategorizedSQLException Mybatis中jdbcType的作用
使用MyBatis框架做更新操作时,在该字段需要更新的内容为空时,就会出现1111错误,也就是无效的列类型,这个时候你就要使用jdbcType.至于什么时候要使用到javaType我还没遇到过,而且我 ...