android - Do images with AsyncTask download parallel or sequentially? -


i made asynctask load images recyclerview. has method downloadimage() call every time @ viewholder. each image should create new asynctask? can't figure out if download parallel or sequentially. (i can't use libraries, must custom)

private static class downloadimage extends asynctask<string, void, bitmap> {      private imageview mbitmapimage;      downloadimage(imageview imageview) {         mbitmapimage = imageview;     }      @override     protected bitmap doinbackground(string... strings) {         string url = strings[0];         bitmap bitmapimage = null;         inputstream in = null;          try {             in = new url(url).openstream();             bitmapimage = bitmapfactory.decodestream(in);             in.close();         } catch (ioexception e) {             e.printstacktrace();         } {             if (in != null) {                 try {                     in.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }         return bitmapimage;     }      @override     protected void onpostexecute(bitmap result) {         if (result != null) {             mbitmapimage.setimagebitmap(result);         } else {             mbitmapimage.setimageresource(r.drawable.loading_movie);         }     } }  public static void downloadimage(string imagelocation, imageview imageview) {     new downloadimage(imageview).execute(movie_poster_url_request + imagelocation); } 

in adapter call this:

void bindmovie(movie movie) {         mmovie = movie;         mmovietitle.settext(movie.gettitle());         mdescription.settext(movie.getoverview());         downloadimage(movie.getposterpath(), mposter);     } 

it depends on version of android system.

but if want sure execute tasks in parallel, use (from support v.4 library): asynctaskcompat.executeparallel(task, params);

in-depth explanation (see accepted answer): running multiple asynctasks @ same time -- not possible?

update:

as stated, asynctaskcompat.executeparallel(task, params); deprecated in api 26, though couldn't find explanation why.

so, docs saying, instead should use asynctask.executeonexecutor(task, params);

to achieve parallel execution:

asynctask.executeonexecutor(asynctask.thread_pool_executor, params);

this method typically used thread_pool_executor allow multiple tasks run in parallel on pool of threads managed asynctask, can use own executor custom behavior.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -