[Android] AsyncImageLoader: add thread pool

Thomas Guillem git at videolan.org
Tue Oct 6 18:32:34 CEST 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Oct  6 16:30:52 2015 +0200| [add427facb0f36aa628bcd949eeb872a7786ead6] | committer: Thomas Guillem

AsyncImageLoader: add thread pool

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=add427facb0f36aa628bcd949eeb872a7786ead6
---

 vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java b/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
index ccf6210..f3ff370 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
@@ -38,6 +38,9 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 public class AsyncImageLoader {
 
@@ -49,14 +52,18 @@ public class AsyncImageLoader {
 
     static ExecutorService executor = Executors.newSingleThreadExecutor();
 
+    /* Maximum one thread that is killed after 2 seconds of inactivity */
+    static ThreadPoolExecutor sThreadPool = new ThreadPoolExecutor(0, 1, 2, TimeUnit.SECONDS,
+                                                                   new LinkedBlockingQueue<Runnable>());
+
     public static void LoadImage(Callable<Bitmap> loader, final ImageUpdater updater, final View target){
         final Future<Bitmap> future = executor.submit(loader);
-        new Thread(new Runnable() {
+        sThreadPool.execute(new Runnable() {
             @Override
             public void run() {
                 handleImage(updater, future, target);
             }
-        }).start();
+        });
     }
 
     public static void LoadAudioCover(Callable<Bitmap> loader, final ViewDataBinding binding, final Activity activity){



More information about the Android mailing list