Merge "Fix Settings creating too many threads unexpectedly" into qt-qpr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
e1c11ca2e7
@@ -18,6 +18,7 @@ package com.android.settingslib.utils;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
@@ -26,7 +27,7 @@ public class ThreadUtils {
|
|||||||
|
|
||||||
private static volatile Thread sMainThread;
|
private static volatile Thread sMainThread;
|
||||||
private static volatile Handler sMainThreadHandler;
|
private static volatile Handler sMainThreadHandler;
|
||||||
private static volatile ExecutorService sSingleThreadExecutor;
|
private static volatile ExecutorService sThreadExecutor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the current thread is the UI thread.
|
* Returns true if the current thread is the UI thread.
|
||||||
@@ -64,10 +65,16 @@ public class ThreadUtils {
|
|||||||
* @Return A future of the task that can be monitored for updates or cancelled.
|
* @Return A future of the task that can be monitored for updates or cancelled.
|
||||||
*/
|
*/
|
||||||
public static Future postOnBackgroundThread(Runnable runnable) {
|
public static Future postOnBackgroundThread(Runnable runnable) {
|
||||||
if (sSingleThreadExecutor == null) {
|
return getThreadExecutor().submit(runnable);
|
||||||
sSingleThreadExecutor = Executors.newSingleThreadExecutor();
|
}
|
||||||
}
|
|
||||||
return sSingleThreadExecutor.submit(runnable);
|
/**
|
||||||
|
* Posts callable in background using shared background thread pool.
|
||||||
|
*
|
||||||
|
* @Return A future of the task that can be monitored for updates or cancelled.
|
||||||
|
*/
|
||||||
|
public static Future postOnBackgroundThread(Callable callable) {
|
||||||
|
return getThreadExecutor().submit(callable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -77,4 +84,11 @@ public class ThreadUtils {
|
|||||||
getUiThreadHandler().post(runnable);
|
getUiThreadHandler().post(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static synchronized ExecutorService getThreadExecutor() {
|
||||||
|
if (sThreadExecutor == null) {
|
||||||
|
sThreadExecutor = Executors.newFixedThreadPool(
|
||||||
|
Runtime.getRuntime().availableProcessors());
|
||||||
|
}
|
||||||
|
return sThreadExecutor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ public class ThreadUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPostOnMainThread_shouldRunOnMainTread() {
|
public void testPostOnMainThread_shouldRunOnMainThread() {
|
||||||
TestRunnable cr = new TestRunnable();
|
TestRunnable cr = new TestRunnable();
|
||||||
ShadowLooper.pauseMainLooper();
|
ShadowLooper.pauseMainLooper();
|
||||||
ThreadUtils.postOnMainThread(cr);
|
ThreadUtils.postOnMainThread(cr);
|
||||||
|
|||||||
Reference in New Issue
Block a user