aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/research/ResearchLog.java
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2013-02-14 14:03:24 -0800
committerKurt Partridge <kep@google.com>2013-03-12 17:09:08 -0700
commit7423005b96b3afc3a6ff55ae40d143920d5a6221 (patch)
tree62368281faceff2b54eca697b2d376dd2b3f8196 /java/src/com/android/inputmethod/research/ResearchLog.java
parentbba39b9b678f4fb00511ba88c12eef9082ecc628 (diff)
downloadlatinime-7423005b96b3afc3a6ff55ae40d143920d5a6221.tar.gz
latinime-7423005b96b3afc3a6ff55ae40d143920d5a6221.tar.xz
latinime-7423005b96b3afc3a6ff55ae40d143920d5a6221.zip
[Lazy1] Switch to blocking log closures
Change-Id: I4daec20b7b47b0d71c5aab6e17cd660015e19e71
Diffstat (limited to 'java/src/com/android/inputmethod/research/ResearchLog.java')
-rw-r--r--java/src/com/android/inputmethod/research/ResearchLog.java47
1 files changed, 32 insertions, 15 deletions
diff --git a/java/src/com/android/inputmethod/research/ResearchLog.java b/java/src/com/android/inputmethod/research/ResearchLog.java
index 9016e23b3..6335b195f 100644
--- a/java/src/com/android/inputmethod/research/ResearchLog.java
+++ b/java/src/com/android/inputmethod/research/ResearchLog.java
@@ -20,11 +20,11 @@ import android.content.Context;
import android.util.JsonWriter;
import android.util.Log;
+import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.define.ProductionFlag;
import java.io.BufferedWriter;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -54,7 +54,6 @@ public class ResearchLog {
private static final String TAG = ResearchLog.class.getSimpleName();
private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
private static final long FLUSH_DELAY_IN_MS = 1000 * 5;
- private static final int ABORT_TIMEOUT_IN_MS = 1000 * 4;
/* package */ final ScheduledExecutorService mExecutor;
/* package */ final File mFile;
@@ -100,7 +99,7 @@ public class ResearchLog {
*
* See class comment for details about {@code JsonWriter} construction.
*/
- public synchronized void close(final Runnable onClosed) {
+ private synchronized void close(final Runnable onClosed) {
mExecutor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
@@ -131,15 +130,22 @@ public class ResearchLog {
mExecutor.shutdown();
}
- private boolean mIsAbortSuccessful;
-
/**
- * Waits for publication requests to finish, closes the {@link JsonWriter}, but then deletes the
- * backing file used for output.
+ * Block until the research log has shut down and spooled out all output or {@code timeout}
+ * occurs.
*
- * See class comment for details about {@code JsonWriter} construction.
+ * @param timeout time to wait for close in milliseconds
+ */
+ public void blockingClose(final long timeout) {
+ close(null);
+ awaitTermination(timeout, TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * Waits for publication requests to finish, closes the JsonWriter, but then deletes the backing
+ * output file.
*/
- public synchronized void abort() {
+ private synchronized void abort() {
mExecutor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
@@ -151,7 +157,7 @@ public class ResearchLog {
}
} finally {
if (mFile != null) {
- mIsAbortSuccessful = mFile.delete();
+ mFile.delete();
}
}
return null;
@@ -161,14 +167,25 @@ public class ResearchLog {
mExecutor.shutdown();
}
- public boolean blockingAbort() throws InterruptedException {
+ /**
+ * Block until the research log has aborted or {@code timeout} occurs.
+ *
+ * @param timeout time to wait for close in milliseconds
+ */
+ public void blockingAbort(final long timeout) {
abort();
- mExecutor.awaitTermination(ABORT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS);
- return mIsAbortSuccessful;
+ awaitTermination(timeout, TimeUnit.MILLISECONDS);
}
- public void awaitTermination(int delay, TimeUnit timeUnit) throws InterruptedException {
- mExecutor.awaitTermination(delay, timeUnit);
+ @UsedForTesting
+ public void awaitTermination(final long delay, final TimeUnit timeUnit) {
+ try {
+ if (!mExecutor.awaitTermination(delay, timeUnit)) {
+ Log.e(TAG, "ResearchLog executor timed out while awaiting terminaion");
+ }
+ } catch (final InterruptedException e) {
+ Log.e(TAG, "ResearchLog executor interrupted while awaiting terminaion", e);
+ }
}
/* package */ synchronized void flush() {