diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-08-31 16:17:04 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-08-31 16:17:04 +0000 |
commit | c39ea3c55fac807c0b98aabdf56c70dc8a49036c (patch) | |
tree | e282668a9587cf6c1ec7b604dea860400c75c6c7 /java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java | |
parent | 68038172793ee0e2ab3e2e56ddfbeb82879d1f58 (diff) | |
parent | 2ca4318cc1ee57dda907ba2069bd61d162b1baef (diff) |
Merge "Update Dialer source to latest internal Google revision."
Diffstat (limited to 'java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java')
-rw-r--r-- | java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java | 121 |
1 files changed, 73 insertions, 48 deletions
diff --git a/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java b/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java index 4b6d81afd..a87bbceb4 100644 --- a/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java +++ b/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java @@ -17,6 +17,7 @@ package com.android.dialer.common.concurrent; import android.app.FragmentManager; +import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.android.dialer.common.Assert; @@ -25,9 +26,13 @@ import com.android.dialer.common.concurrent.DialerExecutor.Builder; import com.android.dialer.common.concurrent.DialerExecutor.FailureListener; import com.android.dialer.common.concurrent.DialerExecutor.SuccessListener; import com.android.dialer.common.concurrent.DialerExecutor.Worker; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; import javax.inject.Inject; /** The production {@link DialerExecutorFactory}. */ @@ -62,16 +67,16 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { throwable -> { throw new RuntimeException(throwable); }; - @Nullable final ExecutorService serialExecutorService; - @Nullable final ExecutorService parallelExecutorService; + @Nullable final ScheduledExecutorService serialExecutorService; + @Nullable final Executor parallelExecutor; BaseTaskBuilder( Worker<InputT, OutputT> worker, - @Nullable ExecutorService serialExecutorService, - @Nullable ExecutorService parallelExecutorService) { + @Nullable ScheduledExecutorService serialExecutorService, + @Nullable Executor parallelExecutor) { this.worker = worker; this.serialExecutorService = serialExecutorService; - this.parallelExecutorService = parallelExecutorService; + this.parallelExecutor = parallelExecutor; } @NonNull @@ -91,6 +96,19 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { /** Convenience class for use by {@link DialerExecutorFactory} implementations. */ public static class UiTaskBuilder<InputT, OutputT> extends BaseTaskBuilder<InputT, OutputT> { + private static final ScheduledExecutorService defaultSerialExecutorService = + Executors.newSingleThreadScheduledExecutor( + new ThreadFactory() { + @Override + public Thread newThread(Runnable runnable) { + LogUtil.i("UiTaskBuilder.newThread", "creating serial thread"); + Thread thread = new Thread(runnable, "UiTaskBuilder-Serial"); + thread.setPriority(5); // Corresponds to Process.THREAD_PRIORITY_DEFAULT + return thread; + } + }); + + private static final Executor defaultParallelExecutorService = AsyncTask.THREAD_POOL_EXECUTOR; private final FragmentManager fragmentManager; private final String id; @@ -102,16 +120,16 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { fragmentManager, id, worker, - null /* serialExecutorService */, - null /* parallelExecutorService */); + defaultSerialExecutorService, + defaultParallelExecutorService); } public UiTaskBuilder( FragmentManager fragmentManager, String id, Worker<InputT, OutputT> worker, - ExecutorService serialExecutor, - ExecutorService parallelExecutor) { + ScheduledExecutorService serialExecutor, + Executor parallelExecutor) { super(worker, serialExecutor, parallelExecutor); this.fragmentManager = fragmentManager; this.id = id; @@ -128,46 +146,36 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { super.successListener, super.failureListener, serialExecutorService, - parallelExecutorService); + parallelExecutor); return new UiDialerExecutor<>(dialerUiTaskFragment); } } /** Convenience class for use by {@link DialerExecutorFactory} implementations. */ public static class NonUiTaskBuilder<InputT, OutputT> extends BaseTaskBuilder<InputT, OutputT> { - private static final ExecutorService defaultSerialExecutorService = - Executors.newSingleThreadExecutor( + private static final ScheduledExecutorService defaultSerialExecutorService = + Executors.newSingleThreadScheduledExecutor( new ThreadFactory() { @Override public Thread newThread(Runnable runnable) { LogUtil.i("NonUiTaskBuilder.newThread", "creating serial thread"); - Thread thread = new Thread(runnable, "NonUiTaskBuilder"); + Thread thread = new Thread(runnable, "NonUiTaskBuilder-Serial"); thread.setPriority(4); // Corresponds to Process.THREAD_PRIORITY_BACKGROUND return thread; } }); - private static final ExecutorService defaultParallelExecutorService = - Executors.newFixedThreadPool( - 5, - new ThreadFactory() { - @Override - public Thread newThread(Runnable runnable) { - LogUtil.i("NonUiTaskBuilder.newThread", "creating parallel thread"); - Thread thread = new Thread(runnable, "NonUiTaskBuilder"); - thread.setPriority(4); // Corresponds to Process.THREAD_PRIORITY_BACKGROUND - return thread; - } - }); + private static final Executor defaultParallelExecutor = + DialerExecutors.getLowPriorityThreadPool(); NonUiTaskBuilder(Worker<InputT, OutputT> worker) { - this(worker, defaultSerialExecutorService, defaultParallelExecutorService); + this(worker, defaultSerialExecutorService, defaultParallelExecutor); } public NonUiTaskBuilder( Worker<InputT, OutputT> worker, - @NonNull ExecutorService serialExecutor, - @NonNull ExecutorService parallelExecutor) { + @NonNull ScheduledExecutorService serialExecutor, + @NonNull Executor parallelExecutor) { super(worker, Assert.isNotNull(serialExecutor), Assert.isNotNull(parallelExecutor)); } @@ -179,7 +187,7 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { super.successListener, super.failureListener, serialExecutorService, - parallelExecutorService); + parallelExecutor); } } @@ -197,6 +205,11 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { } @Override + public void executeSerialWithWait(@Nullable InputT input, long waitMillis) { + dialerUiTaskFragment.executeSerialWithWait(input, waitMillis); + } + + @Override public void executeParallel(@Nullable InputT input) { dialerUiTaskFragment.executeParallel(input); } @@ -214,47 +227,59 @@ public class DefaultDialerExecutorFactory implements DialerExecutorFactory { private final SuccessListener<OutputT> successListener; private final FailureListener failureListener; - private final ExecutorService serialExecutorService; - private final ExecutorService parallelExecutorService; + private final ScheduledExecutorService serialExecutorService; + private final Executor parallelExecutor; + + private ScheduledFuture<?> scheduledFuture; NonUiDialerExecutor( Worker<InputT, OutputT> worker, SuccessListener<OutputT> successListener, FailureListener failureListener, - ExecutorService serialExecutorService, - ExecutorService parallelExecutorService) { + ScheduledExecutorService serialExecutorService, + Executor parallelExecutor) { this.worker = worker; this.successListener = successListener; this.failureListener = failureListener; this.serialExecutorService = serialExecutorService; - this.parallelExecutorService = parallelExecutorService; + this.parallelExecutor = parallelExecutor; } @Override public void executeSerial(@Nullable InputT input) { - executeOnCustomExecutorService(serialExecutorService, input); + serialExecutorService.execute(() -> run(input)); + } + + @Override + public void executeSerialWithWait(@Nullable InputT input, long waitMillis) { + if (scheduledFuture != null) { + LogUtil.i("NonUiDialerExecutor.executeSerialWithWait", "cancelling waiting task"); + scheduledFuture.cancel(false /* mayInterrupt */); + } + scheduledFuture = + serialExecutorService.schedule(() -> run(input), waitMillis, TimeUnit.MILLISECONDS); } @Override public void executeParallel(@Nullable InputT input) { - executeOnCustomExecutorService(parallelExecutorService, input); + parallelExecutor.execute(() -> run(input)); } @Override public void executeOnCustomExecutorService( @NonNull ExecutorService executorService, @Nullable InputT input) { - Assert.isNotNull(executorService) - .execute( - () -> { - OutputT output; - try { - output = worker.doInBackground(input); - } catch (Throwable throwable) { - ThreadUtil.postOnUiThread(() -> failureListener.onFailure(throwable)); - return; - } - ThreadUtil.postOnUiThread(() -> successListener.onSuccess(output)); - }); + Assert.isNotNull(executorService).execute(() -> run(input)); + } + + private void run(@Nullable InputT input) { + OutputT output; + try { + output = worker.doInBackground(input); + } catch (Throwable throwable) { + ThreadUtil.postOnUiThread(() -> failureListener.onFailure(throwable)); + return; + } + ThreadUtil.postOnUiThread(() -> successListener.onSuccess(output)); } } } |