博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AsyncTask測试多任务
阅读量:6119 次
发布时间:2019-06-21

本文共 12638 字,大约阅读时间需要 42 分钟。

本人进行过模拟測试,发现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
{ 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));
//有益休眠 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
{ 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): onPreExecute12-17 05:24:57.665: D/dalvikvm(25755): create interp thread : stack size=32KB12-17 05:24:57.665: D/dalvikvm(25755): create new thread12-17 05:24:57.666: D/dalvikvm(25755): new thread created12-17 05:24:57.666: D/dalvikvm(25755): update thread list12-17 05:24:57.666: D/dalvikvm(25755): threadid=16: interp stack at 0x5e67700012-17 05:24:57.666: D/dalvikvm(25755): threadid=16: created from interp12-17 05:24:57.666: D/dalvikvm(25755): start new thread12-17 05:24:57.666: D/dalvikvm(25755): threadid=16: notify debugger12-17 05:24:57.666: D/dalvikvm(25755): threadid=16 (AsyncTask #5): calling run()12-17 05:24:57.666: D/MainActivity(25755): onPreExecute12-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@42258fc812-17 05:24:57.669: D/MainActivity(25755): onPreExecute12-17 05:24:57.669: D/MainActivity(25755): onPreExecute12-17 05:24:57.731: V/InputMethodManager(25755): onWindowFocus: null softInputMode=288 first=true flags=#181010012-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=#10412-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:478412-17 05:24:58.966: D/MainActivity(25755): MyTask:1 AsyncTask #5 id:478412-17 05:24:59.643: D/MainActivity(25755): MyTask:2 AsyncTask #5 id:478412-17 05:25:00.276: D/MainActivity(25755): MyTask:3 AsyncTask #5 id:478412-17 05:25:00.946: D/MainActivity(25755): MyTask:4 AsyncTask #5 id:478412-17 05:25:01.589: D/MainActivity(25755): MyTask:5 AsyncTask #5 id:478412-17 05:25:02.224: D/MainActivity(25755): MyTask:6 AsyncTask #5 id:478412-17 05:25:02.842: D/MainActivity(25755): MyTask:7 AsyncTask #5 id:478412-17 05:25:03.464: D/MainActivity(25755): MyTask:8 AsyncTask #5 id:478412-17 05:25:04.120: D/MainActivity(25755): MyTask:9 AsyncTask #5 id:478412-17 05:25:04.123: I/System.out(25755): [CDS]rx timeout:012-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:true12-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:512-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] connected12-17 05:25:04.547: I/System.out(25755): [CDS]rx timeout:012-17 05:25:04.613: D/MainActivity(25755): get image is ok12-17 05:25:04.643: D/dalvikvm(25755): GC_FOR_ALLOC freed 677K (925), 32% free 3736K/5484K, paused 23ms, total 28ms12-17 05:25:04.646: D/skia(25755): Flag is not 1012-17 05:25:04.647: D/MainActivity(25755): onPostExecute12-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@42258fc812-17 05:25:04.668: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.14, dur:6910.24, max:6910.24, min:6910.2412-17 05:25:04.668: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:6910.2412-17 05:25:05.485: D/MainActivity(25755): MyTask2:0 AsyncTask #5 id:478412-17 05:25:06.323: D/MainActivity(25755): MyTask2:1 AsyncTask #5 id:478412-17 05:25:07.217: D/MainActivity(25755): MyTask2:2 AsyncTask #5 id:478412-17 05:25:08.107: D/MainActivity(25755): MyTask2:3 AsyncTask #5 id:478412-17 05:25:09.002: D/MainActivity(25755): MyTask2:4 AsyncTask #5 id:478412-17 05:25:09.881: D/MainActivity(25755): MyTask2:5 AsyncTask #5 id:478412-17 05:25:10.771: D/MainActivity(25755): MyTask2:6 AsyncTask #5 id:478412-17 05:25:11.579: D/MainActivity(25755): MyTask2:7 AsyncTask #5 id:478412-17 05:25:12.445: D/MainActivity(25755): MyTask2:8 AsyncTask #5 id:478412-17 05:25:13.299: D/MainActivity(25755): MyTask2:9 AsyncTask #5 id:478412-17 05:25:13.300: I/System.out(25755): [CDS]rx timeout:012-17 05:25:13.586: D/MainActivity(25755): MyTask2:get image is ok12-17 05:25:13.610: D/dalvikvm(25755): GC_FOR_ALLOC freed 688K (1136), 33% free 3689K/5484K, paused 21ms, total 21ms12-17 05:25:13.613: D/skia(25755): Flag is not 1012-17 05:25:13.613: D/MainActivity(25755): onPostExecute12-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@42258fc812-17 05:25:13.630: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.11, dur:8961.83, max:8961.83, min:8961.8312-17 05:25:13.630: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:8961.8312-17 05:25:14.455: D/MainActivity(25755): MyTask2:0 AsyncTask #5 id:478412-17 05:25:15.304: D/MainActivity(25755): MyTask2:1 AsyncTask #5 id:478412-17 05:25:16.104: D/MainActivity(25755): MyTask2:2 AsyncTask #5 id:478412-17 05:25:17.008: D/MainActivity(25755): MyTask2:3 AsyncTask #5 id:478412-17 05:25:17.887: D/MainActivity(25755): MyTask2:4 AsyncTask #5 id:478412-17 05:25:18.737: D/MainActivity(25755): MyTask2:5 AsyncTask #5 id:478412-17 05:25:19.584: D/MainActivity(25755): MyTask2:6 AsyncTask #5 id:478412-17 05:25:20.399: D/MainActivity(25755): MyTask2:7 AsyncTask #5 id:478412-17 05:25:21.281: D/MainActivity(25755): MyTask2:8 AsyncTask #5 id:478412-17 05:25:22.130: D/MainActivity(25755): MyTask2:9 AsyncTask #5 id:478412-17 05:25:22.132: I/System.out(25755): [CDS]rx timeout:012-17 05:25:22.178: D/MainActivity(25755): MyTask2:get image is ok12-17 05:25:22.202: D/dalvikvm(25755): GC_FOR_ALLOC freed 641K (300), 33% free 3689K/5484K, paused 21ms, total 22ms12-17 05:25:22.208: D/skia(25755): Flag is not 1012-17 05:25:22.209: D/MainActivity(25755): onPostExecute12-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@42258fc812-17 05:25:22.219: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.12, dur:8589.36, max:8589.36, min:8589.3612-17 05:25:22.219: I/SurfaceTextureClient(25755): [STC::queueBuffer] this:0x5eee0028, api:1, last queue time elapsed:8589.3612-17 05:25:22.839: D/MainActivity(25755): MyTask:0 AsyncTask #5 id:478412-17 05:25:23.519: D/MainActivity(25755): MyTask:1 AsyncTask #5 id:478412-17 05:25:24.176: D/MainActivity(25755): MyTask:2 AsyncTask #5 id:478412-17 05:25:24.786: D/MainActivity(25755): MyTask:3 AsyncTask #5 id:478412-17 05:25:25.440: D/MainActivity(25755): MyTask:4 AsyncTask #5 id:478412-17 05:25:26.134: D/MainActivity(25755): MyTask:5 AsyncTask #5 id:478412-17 05:25:26.740: D/MainActivity(25755): MyTask:6 AsyncTask #5 id:478412-17 05:25:27.367: D/MainActivity(25755): MyTask:7 AsyncTask #5 id:478412-17 05:25:27.988: D/MainActivity(25755): MyTask:8 AsyncTask #5 id:478412-17 05:25:28.617: D/MainActivity(25755): MyTask:9 AsyncTask #5 id:478412-17 05:25:28.618: I/System.out(25755): [CDS]rx timeout:012-17 05:25:28.658: D/MainActivity(25755): get image is ok12-17 05:25:28.681: D/dalvikvm(25755): GC_FOR_ALLOC freed 641K (299), 33% free 3689K/5484K, paused 20ms, total 21ms12-17 05:25:28.683: D/skia(25755): Flag is not 1012-17 05:25:28.684: D/MainActivity(25755): onPostExecute12-17 05:25:28.694: I/SurfaceTextureClient(25755): [STC::queueBuffer] (this:0x5eee0028) fps:0.15, dur:6474.71, max:6474.71, min:6474.7112-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更新? 如有朋友知道详细理由请指出,,谢谢。

转载地址:http://exlka.baihongyu.com/

你可能感兴趣的文章
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
P127、面试题20:顺时针打印矩阵
查看>>
JS图片跟着鼠标跑效果
查看>>
[SCOI2005][BZOJ 1084]最大子矩阵
查看>>
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
【FJOI2015】金币换位问题
查看>>
数学之美系列二十 -- 自然语言处理的教父 马库斯
查看>>
Android实现自定义位置无标题Dialog
查看>>
面试总结
查看>>
Chrome浏览器播放HTML5音频没声音的解决方案
查看>>
easyui datagrid 行编辑功能
查看>>
类,对象与实例变量
查看>>
HDU 2818 (矢量并查集)
查看>>
【转】php字符串加密解密
查看>>
22. linux 常用命令
查看>>
ASP.Net 使用GridView模板删除一行的用法
查看>>
(十六)字段表集合
查看>>
JPGraph
查看>>