am 26628eeb: Merge "Add thread name for PrioritizedSerialExecutor."

* commit '26628eeb4b4a2ffdd6b1912e4bcefc83ac802ceb':
  Add thread name for PrioritizedSerialExecutor.
main
Keisuke Kuroyanagi 2014-05-27 07:26:33 +00:00 committed by Android Git Automerger
commit c999d89b3d
2 changed files with 22 additions and 7 deletions

View File

@ -28,14 +28,14 @@ public class ExecutorUtils {
new ConcurrentHashMap<>();
/**
* Gets the executor for the given dictionary name.
* Gets the executor for the given id.
*/
public static PrioritizedSerialExecutor getExecutor(final String dictName) {
PrioritizedSerialExecutor executor = sExecutorMap.get(dictName);
public static PrioritizedSerialExecutor getExecutor(final String id) {
PrioritizedSerialExecutor executor = sExecutorMap.get(id);
if (executor == null) {
synchronized(sExecutorMap) {
executor = new PrioritizedSerialExecutor();
sExecutorMap.put(dictName, executor);
executor = new PrioritizedSerialExecutor(id);
sExecutorMap.put(id, executor);
}
}
return executor;

View File

@ -21,6 +21,7 @@ import com.android.inputmethod.annotations.UsedForTesting;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -40,12 +41,26 @@ public class PrioritizedSerialExecutor {
// The task which is running now.
private Runnable mActive;
public PrioritizedSerialExecutor() {
private static class ThreadFactoryWithId implements ThreadFactory {
private final String mId;
public ThreadFactoryWithId(final String id) {
mId = id;
}
@Override
public Thread newThread(final Runnable r) {
return new Thread(r, TAG + " - " + mId);
}
}
public PrioritizedSerialExecutor(final String id) {
mTasks = new ConcurrentLinkedQueue<>();
mPrioritizedTasks = new ConcurrentLinkedQueue<>();
mIsShutdown = false;
mThreadPoolExecutor = new ThreadPoolExecutor(1 /* corePoolSize */, 1 /* maximumPoolSize */,
0 /* keepAliveTime */, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
0 /* keepAliveTime */, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1),
new ThreadFactoryWithId(id));
}
/**