Make more threads in the executor for background tasks

Let injected preferences load data simultaneously without being blocked
by other ones.

Bug: 141694556
Test: make RunSettingsLibRoboTests -j
Change-Id: If10ef8c367492dcbd8aba66c1b01036fc79a8093
This commit is contained in:
Jason Chiu
2019-09-27 13:48:00 +08:00
parent cb364f996f
commit 92de722b71
2 changed files with 6 additions and 5 deletions

View File

@@ -26,7 +26,7 @@ public class ThreadUtils {
private static volatile Thread sMainThread;
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.
@@ -64,10 +64,11 @@ public class ThreadUtils {
* @Return A future of the task that can be monitored for updates or cancelled.
*/
public static Future postOnBackgroundThread(Runnable runnable) {
if (sSingleThreadExecutor == null) {
sSingleThreadExecutor = Executors.newSingleThreadExecutor();
if (sThreadExecutor == null) {
sThreadExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
}
return sSingleThreadExecutor.submit(runnable);
return sThreadExecutor.submit(runnable);
}
/**

View File

@@ -50,7 +50,7 @@ public class ThreadUtilsTest {
}
@Test
public void testPostOnMainThread_shouldRunOnMainTread() {
public void testPostOnMainThread_shouldRunOnMainThread() {
TestRunnable cr = new TestRunnable();
ShadowLooper.pauseMainLooper();
ThreadUtils.postOnMainThread(cr);