ExecutorUtils lets use schedule a runnable chain.
Bug 19625976. Change-Id: Iebbef7fed57a381498301dcf26fefa27d06802f7main
parent
f9e2d271ca
commit
bd1bc4067f
|
@ -56,8 +56,8 @@ public class ContactsContentObserver implements Runnable {
|
||||||
mContentObserver = new ContentObserver(null /* handler */) {
|
mContentObserver = new ContentObserver(null /* handler */) {
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean self) {
|
public void onChange(boolean self) {
|
||||||
ExecutorUtils.getBackgroundExecutor()
|
// TODO(zivkovic): Schedule a separate task to reset the decoder.
|
||||||
.execute(ContactsContentObserver.this);
|
ExecutorUtils.getBackgroundExecutor().execute(ContactsContentObserver.this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final ContentResolver contentResolver = mContext.getContentResolver();
|
final ContentResolver contentResolver = mContext.getContentResolver();
|
||||||
|
|
|
@ -23,7 +23,9 @@ import com.android.inputmethod.annotations.UsedForTesting;
|
||||||
import java.lang.Thread.UncaughtExceptionHandler;
|
import java.lang.Thread.UncaughtExceptionHandler;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utilities to manage executors.
|
* Utilities to manage executors.
|
||||||
|
@ -35,33 +37,6 @@ public class ExecutorUtils {
|
||||||
private static final ScheduledExecutorService sExecutorService =
|
private static final ScheduledExecutorService sExecutorService =
|
||||||
Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());
|
Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());
|
||||||
|
|
||||||
@UsedForTesting
|
|
||||||
private static ScheduledExecutorService sExecutorServiceForTests;
|
|
||||||
|
|
||||||
@UsedForTesting
|
|
||||||
public static void setExecutorServiceForTests(
|
|
||||||
final ScheduledExecutorService executorServiceForTests) {
|
|
||||||
sExecutorServiceForTests = executorServiceForTests;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return scheduled executor service used to run background tasks
|
|
||||||
*/
|
|
||||||
public static ScheduledExecutorService getBackgroundExecutor() {
|
|
||||||
if (sExecutorServiceForTests != null) {
|
|
||||||
return sExecutorServiceForTests;
|
|
||||||
}
|
|
||||||
return sExecutorService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shutdowns all executors and removes all executors from the executor map for testing.
|
|
||||||
*/
|
|
||||||
@UsedForTesting
|
|
||||||
public static void shutdownAllExecutors() {
|
|
||||||
sExecutorService.execute(new ExecutorShutdown(sExecutorService));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ExecutorFactory implements ThreadFactory {
|
private static class ExecutorFactory implements ThreadFactory {
|
||||||
@Override
|
@Override
|
||||||
public Thread newThread(final Runnable runnable) {
|
public Thread newThread(final Runnable runnable) {
|
||||||
|
@ -76,16 +51,51 @@ public class ExecutorUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ExecutorShutdown implements Runnable {
|
@UsedForTesting
|
||||||
private final ScheduledExecutorService mExecutor;
|
private static ScheduledExecutorService sExecutorServiceForTests;
|
||||||
|
|
||||||
public ExecutorShutdown(final ScheduledExecutorService executor) {
|
@UsedForTesting
|
||||||
mExecutor = executor;
|
public static void setExecutorServiceForTests(
|
||||||
|
final ScheduledExecutorService executorServiceForTests) {
|
||||||
|
sExecutorServiceForTests = executorServiceForTests;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Public methods used to schedule a runnable for execution.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return scheduled executor service used to run background tasks
|
||||||
|
*/
|
||||||
|
public static ScheduledExecutorService getBackgroundExecutor() {
|
||||||
|
if (sExecutorServiceForTests != null) {
|
||||||
|
return sExecutorServiceForTests;
|
||||||
|
}
|
||||||
|
return sExecutorService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Runnable chain(final Runnable... runnables) {
|
||||||
|
return new RunnableChain(runnables);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class RunnableChain implements Runnable {
|
||||||
|
private final Runnable[] mRunnables;
|
||||||
|
|
||||||
|
private RunnableChain(final Runnable... runnables) {
|
||||||
|
if (runnables == null || runnables.length == 0) {
|
||||||
|
throw new IllegalArgumentException("Attempting to construct an empty chain");
|
||||||
|
}
|
||||||
|
mRunnables = runnables;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
mExecutor.shutdown();
|
for (Runnable runnable : mRunnables) {
|
||||||
|
if (Thread.interrupted()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue