Suppress cancelling if process already terminated.

Bug: 123529934
Test: unit tests
Bug: 119259967
Change-Id: I0e78dba6de27e4a0f5d60f7f806c4d58398150c6
Merged-In: I0e78dba6de27e4a0f5d60f7f806c4d58398150c6
diff --git a/src/com/android/tradefed/util/RunUtil.java b/src/com/android/tradefed/util/RunUtil.java
index 62f73ee..b1bd220 100644
--- a/src/com/android/tradefed/util/RunUtil.java
+++ b/src/com/android/tradefed/util/RunUtil.java
@@ -546,7 +546,7 @@
         private OutputStream stdOut = null;
         private OutputStream stdErr = null;
         private final boolean mCloseStreamAfterRun;
-        private Object mLock = new Object();
+        private final Object mLock = new Object();
         private boolean mCancelled = false;
 
         RunnableResult(final CommandResult result, final String input,
@@ -598,7 +598,7 @@
             Thread stdoutThread = null;
             Thread stderrThread = null;
             synchronized (mLock) {
-                if (mCancelled == true) {
+                if (mCancelled) {
                     // if cancel() was called before run() took the lock, we do not even attempt
                     // to run.
                     return false;
@@ -676,7 +676,7 @@
         public void cancel() {
             mCancelled = true;
             synchronized (mLock) {
-                if (mProcess == null) {
+                if (mProcess == null || !mProcess.isAlive()) {
                     return;
                 }
                 CLog.i("Cancelling the process execution");