Asynctask is not suitable for multitasking, and for long-term asynchronous tasks

Source: Internet
Author: User
Tags skia

I have simulated tests and found that Asynctask is not suitable for multitasking and long-term asynchronous tasks because only one asynctask can be run at a time, and if running multiple other tasks will wait

The following is confirmed by a code example and a log print.

The following expands 2 Asynctask, called 2 times, and has been called four times.


public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//4 asynchronous runs together, 2 different implementation objects 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 ();} @Overrideprotected void OnPostExecute (Bitmap result) {LOG.D ("mainactivity", "OnPostExecute"); Updateimageview (Result );} @Overrideprotected void OnPreExecute () {log.d ("mainactivity", "OnPreExecute");} @Overrideprotected Bitmap doinbackground (String ... args) {log.e ("mainactivity", "MyTask2:" + thread_pool_executor);// Static objects are the same, executing the thread pool LOG.E ("Mainactivity", "MyTask2:" + serial_executor); Static objects are the same, string website = Args[0] that performs the task; HttpURLConnection conn = NuLl;inputstream ins = null;try {for (int i = 0; i < i++) {thread.sleep (int) (Math.random () * 100 + 800)); <span style= "font-family:arial, Helvetica, Sans-serif;" >//intentionally sleeps </span>log.d ("Mainactivity", "MyTask2:" + i + "" + Thread.CurrentThread (). GetName () + "ID:" + thread.curr Entthread (). GetId ());} URL url = new URL (website), conn = (httpurlconnection) url.openconnection (); conn.setconnecttimeout (5000); Conn.setdoinput (True); if (conn.getresponsecode () = =) {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 Blocke.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 ();} @Overrideprotected void OnPostExecute (Bitmap result) {LOG.D ("mainactivity", "OnPostExecute"); Updateimageview (result); @Overrideprotected void OnPreExecute () {log.d ("mainactivity", "OnPreExecute");} @Overrideprotected Bitmap doinbackground (String ... args) {log.e ("mainactivity", "MyTask:" + thread_pool_executor);  LOG.E ("Mainactivity", "MyTask:" + serial_executor); for (int i = 0; i < i++) {try {thread.sleep (int) (Math.random () * 100 + 600));//Intentional hibernation} 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.openco Nnection (); Conn.setconnecttimeout () Conn.setdoinput (true); if (conn.getresponsecode () = =) {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) findviewbyi D (r.id.img); Img.setimagebitmap (result);}}}


4 Asynchronous run discovery, the first must run out to run the next one? Just I understand the wrong, or the use of a problem, if a friend knows please correct me.


Print the log as follows:


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/da LVIKVM (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): onPreExecute< Span style= "color: #ff0000;" &GT;12-17 05:24:57.666:e/mainactivity (25755): mytask:[email protected][running, pool size = 5, Active threads = 1, q ueued tasks = 0, completed tasks = 4]12-17 05:24:57.666:e/mainactivity (25755): mytask:[email protected]</span& GT;12-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 [email protected] controlflags= #10412 -17 05:24:57.733:v/inputmethodmanager (25755): Starting Input:bind result=inputbindresult{[email protected]0 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.12 0: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 (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 was not 1012-17 05:25:04.647:d/mainactivity (25755): OnPostExecute <span style= "color: #ff0000;"  &GT;12-17 05:25:04.648:e/mainactivity (25755): mytask2:[email protected][running, pool size = 5, Active threads = 1, Queued tasks = 0, completed tasks = 5]12-17 05:25:04.648:e/mainactivity (25755): Mytask2:[email protected]</span &GT;12-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% fre E 3689k/5484k, paused 21ms, total 21ms12-17 05:25:13.613:d/skia (25755): Flag was not 1012-17 05:25:13.613:d/mainactivity ( 25755): Onpostexecute<span style= "color: #ff6666;"&GT;12-17 05:25:13.615:e/mainactivity (25755): mytask2:[email protected][running, pool size = 5, Active threads = 1, Queued tasks = 0, completed tasks = 6]12-17 05:25:13.615:e/mainactivity (25755): Mytask2:[email protected]</span &GT;12-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 (+), 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): Onpostexecute<span style= "COLOR: #ff0000;" &GT;12-17 05:25:22.211:e/mainactivity (25755): mytask:[email protected][running, pool size = 5, Active threads = 1, q ueued tasks = 0, completed tasks = 7]12-17 05:25:22.211:e/mainactivity (25755): mytask:[email protected]</span& GT;12-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 was not 1012-17 0 5: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 found that only 1 threads are running at a time and can only run once at a time. The scenario is that the thread is reused,

The running thread name and ID are the same on the phone, and running the ID and name on the emulator is not the same as it was executed sequentially, and there was no case of running multiple tasks at the same time.

Individual testing found that asynctask can only be used once, if the instantiation of multiple asynctask does not improve the efficiency of operation, can not take advantage of multi-tasking situation. Don't know if this is the case, Google designed this asynctask only to perform an asynchronous task, and UI updates? If a friend knows the specific reasons, please point out, thank you.


Asynctask is not suitable for multitasking, and for long-term asynchronous tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.