[Android] Lower thread priority to background tasks

Geoffrey Métais git at videolan.org
Thu Apr 21 10:53:11 CEST 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Apr 21 10:41:34 2016 +0200| [4e077641435aadfe88c7a3f1cda927a81eba0250] | committer: Geoffrey Métais

Lower thread priority to background tasks

> https://code.videolan.org/videolan/vlc-android/commit/4e077641435aadfe88c7a3f1cda927a81eba0250
---

 vlc-android/src/org/videolan/vlc/VLCApplication.java     | 12 +++++++++++-
 vlc-android/src/org/videolan/vlc/media/MediaLibrary.java |  2 ++
 vlc-android/src/org/videolan/vlc/media/Thumbnailer.java  |  4 +++-
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index e39333c..613fd86 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -25,6 +25,7 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.os.Process;
 import android.preference.PreferenceManager;
 import android.support.v4.app.DialogFragment;
 import android.support.v4.util.SimpleArrayMap;
@@ -43,6 +44,7 @@ import org.videolan.vlc.util.VLCInstance;
 import java.util.Calendar;
 import java.util.Locale;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
@@ -61,7 +63,15 @@ public class VLCApplication extends Application {
 
     /* Up to 2 threads maximum, inactive threads are killed after 2 seconds */
     private ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(0, 2, 2, TimeUnit.SECONDS,
-                                                                    new LinkedBlockingQueue<Runnable>());
+            new LinkedBlockingQueue<Runnable>(), THREAD_FACTORY);
+    public static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
+        @Override
+        public Thread newThread(Runnable runnable) {
+            Thread thread = new Thread(runnable);
+            thread.setPriority(Process.THREAD_PRIORITY_DEFAULT+Process.THREAD_PRIORITY_LESS_FAVORABLE);
+            return thread;
+        }
+    };
 
     private static int sDialogCounter = 0;
 
diff --git a/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java
index f0e63a6..cafca4e 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java
@@ -24,6 +24,7 @@ import android.net.Uri;
 import android.os.Environment;
 import android.os.Handler;
 import android.os.Message;
+import android.os.Process;
 import android.support.v4.util.ArrayMap;
 import android.text.TextUtils;
 import android.util.Log;
@@ -111,6 +112,7 @@ public class MediaLibrary {
             isStopping = false;
             MediaUtils.actionScanStart();
             mLoadingThread = new Thread(new GetMediaItemsRunnable());
+            mLoadingThread.setPriority(Process.THREAD_PRIORITY_DEFAULT+Process.THREAD_PRIORITY_LESS_FAVORABLE);
             mLoadingThread.start();
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/media/Thumbnailer.java b/vlc-android/src/org/videolan/vlc/media/Thumbnailer.java
index 504a8dc..94e8bc0 100644
--- a/vlc-android/src/org/videolan/vlc/media/Thumbnailer.java
+++ b/vlc-android/src/org/videolan/vlc/media/Thumbnailer.java
@@ -39,6 +39,7 @@ import org.videolan.vlc.util.VLCInstance;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.Config;
+import android.os.Process;
 import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.Display;
@@ -67,8 +68,9 @@ public class Thumbnailer implements Runnable {
     public void start(IVideoBrowser videoBrowser) {
         isStopping = false;
         if (mThread == null || mThread.getState() == State.TERMINATED) {
-            mVideoBrowser = new WeakReference<IVideoBrowser>(videoBrowser);
+            mVideoBrowser = new WeakReference<>(videoBrowser);
             mThread = new Thread(this);
+            mThread.setPriority(Process.THREAD_PRIORITY_DEFAULT+Process.THREAD_PRIORITY_LESS_FAVORABLE);
             mThread.start();
         }
     }



More information about the Android mailing list