Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2367f8f6 authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi Committed by Android (Google) Code Review
Browse files

Merge "Simplify asyncFlushBinaryDictionary."

parents 5c1416e4 570602a0
Loading
Loading
Loading
Loading
+2 −8
Original line number Original line Diff line number Diff line
@@ -40,7 +40,6 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;


/**
/**
 * Abstract base class for an expandable dictionary that can be created and updated dynamically
 * Abstract base class for an expandable dictionary that can be created and updated dynamically
@@ -101,9 +100,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
    /* A extension for a binary dictionary file. */
    /* A extension for a binary dictionary file. */
    protected static final String DICT_FILE_EXTENSION = ".dict";
    protected static final String DICT_FILE_EXTENSION = ".dict";


    private final AtomicReference<Runnable> mUnfinishedFlushingTask =
            new AtomicReference<Runnable>();

    /**
    /**
     * Abstract method for loading initial contents of a given dictionary.
     * Abstract method for loading initial contents of a given dictionary.
     */
     */
@@ -561,14 +557,12 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
     * Flush binary dictionary to dictionary file.
     * Flush binary dictionary to dictionary file.
     */
     */
    public void asyncFlushBinaryDictionary() {
    public void asyncFlushBinaryDictionary() {
        final Runnable newTask = new Runnable() {
        ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
            @Override
            @Override
            public void run() {
            public void run() {
                flushDictionaryLocked();
                flushDictionaryLocked();
            }
            }
        };
        });
        final Runnable oldTask = mUnfinishedFlushingTask.getAndSet(newTask);
        ExecutorUtils.getExecutor(mDictName).replaceAndExecute(oldTask, newTask);
    }
    }


    // TODO: Implement BinaryDictionary.isInDictionary().
    // TODO: Implement BinaryDictionary.isInDictionary().
+0 −33
Original line number Original line Diff line number Diff line
@@ -46,16 +46,6 @@ public class PrioritizedSerialExecutor {
                0 /* keepAliveTime */, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
                0 /* keepAliveTime */, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
    }
    }


    /**
     * Clears all queued tasks.
     */
    public void clearAllTasks() {
        synchronized(mLock) {
            mTasks.clear();
            mPrioritizedTasks.clear();
        }
    }

    /**
    /**
     * Enqueues the given task into the task queue.
     * Enqueues the given task into the task queue.
     * @param r the enqueued task
     * @param r the enqueued task
@@ -120,33 +110,10 @@ public class PrioritizedSerialExecutor {
        }
        }
    }
    }


    public void remove(final Runnable r) {
        synchronized(mLock) {
            mTasks.remove(r);
            mPrioritizedTasks.remove(r);
        }
    }

    public void replaceAndExecute(final Runnable oldTask, final Runnable newTask) {
        synchronized(mLock) {
            if (oldTask != null) remove(oldTask);
            execute(newTask);
        }
    }

    public void shutdown() {
    public void shutdown() {
        synchronized(mLock) {
        synchronized(mLock) {
            mIsShutdown = true;
            mIsShutdown = true;
            mThreadPoolExecutor.shutdown();
            mThreadPoolExecutor.shutdown();
        }
        }
    }
    }

    public boolean isTerminated() {
        synchronized(mLock) {
            if (!mIsShutdown) {
                return false;
            }
            return mPrioritizedTasks.isEmpty() && mTasks.isEmpty() && mActive == null;
        }
    }
}
}