AsyncTask is not suitable for multi-task and long-term asynchronous tasks.
I conducted a simulated test and found that AsyncTask is not suitable for multi-task and long-term asynchronous tasks, because only one AsyncTask can be run at a time. If you run multiple other tasks, you will wait.
The following code examples and log printing are verified.
The following two AsyncTask parameters are extended, which are called twice and called four times in total.
Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // 4 run asynchronously, 2 different implementation objects new mytask(cmd.exe cute ("http://www.baidu.com/img/bdlogo.png"); new mytask2(cmd.exe cute ("http://www.baidu.com/img/bdlogo.png "); new mytask2(cmd.exe cute ("http://www.baidu.com/img/bdlogo.png"); new mytask(cmd.exe cut E ("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 ("Main Activity "," MyTask2: "+ THREAD_POOL_EXECUTOR); // static objects are all the same, and the thread pool Log is executed. e ("MainActivity", "MyTask2:" + SERIAL_EXECUTOR); // static objects are all the same. 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;"> // deliberately sleep </span> Log. d ("MainActivity", "MyTa Sk2: "+ 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 (MalformedURLE Xception 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 <10; I ++) {try {Thread. sleep (int) (Math. random () * 100 + 600); // deliberately sleep} 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. printS TackTrace ();} 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 );}}}
Four asynchronous run discoveries, the first must be completed before the next one can be run? I just don't understand it, but I still have usage problems. If you know something, please correct it.
The print log is 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/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): 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): 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): 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.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): 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.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): 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.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
It is found that only one thread is running at a time in the ThreadPool, and only one time can be run at a time. The preceding running condition is that the thread is reused,
The names and IDs of running threads on the mobile phone are the same. running IDs and names on the simulator is executed in sequence even if they are different, and no tasks can be run at the same time.
After testing, I found that AsyncTask can only be used once. If multiple AsyncTask instances are instantiated, the running efficiency cannot be improved and multi-task tasks cannot be used. I don't know if this is the case. google designed this AsyncTask to only execute an asynchronous task and UI update? If a friend knows the specific reason, please point out, thank you.