Ensure executor threads are daemon

Do not use parallelization if only one device to collect.

Test: unit tests
Bug: 152226657
Change-Id: Ie8e0bee98d65e792786b807b1635bdd5cdcb9041
diff --git a/src/com/android/tradefed/invoker/TestInvocation.java b/src/com/android/tradefed/invoker/TestInvocation.java
index db30c58..44b9f28 100644
--- a/src/com/android/tradefed/invoker/TestInvocation.java
+++ b/src/com/android/tradefed/invoker/TestInvocation.java
@@ -295,7 +295,14 @@
                 }
             }
             if (bugreportName != null) {
-                if (badDevice == null) {
+                if (context.getDevices().size() == 1 || badDevice != null) {
+                    ITestDevice collectBugreport = badDevice;
+                    if (collectBugreport == null) {
+                        collectBugreport = context.getDevices().get(0);
+                    }
+                    // If we have identified a faulty device only take the bugreport on it.
+                    takeBugreport(collectBugreport, listener, bugreportName);
+                } else if (context.getDevices().size() > 1) {
                     ParallelDeviceExecutor<Boolean> executor =
                             new ParallelDeviceExecutor<>(context.getDevices());
                     List<Callable<Boolean>> callableTasks = new ArrayList<>();
@@ -310,9 +317,6 @@
                     }
                     // Capture the bugreports best effort, ignore the results.
                     executor.invokeAll(callableTasks, 5, TimeUnit.MINUTES);
-                } else {
-                    // If we have identified a faulty device only take the bugreport on it.
-                    takeBugreport(badDevice, listener, bugreportName);
                 }
             }
             // Save the device executeShellCommand logs
diff --git a/src/com/android/tradefed/util/executor/ParallelDeviceExecutor.java b/src/com/android/tradefed/util/executor/ParallelDeviceExecutor.java
index a03ef32..c176bdb 100644
--- a/src/com/android/tradefed/util/executor/ParallelDeviceExecutor.java
+++ b/src/com/android/tradefed/util/executor/ParallelDeviceExecutor.java
@@ -25,6 +25,7 @@
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
@@ -48,7 +49,17 @@
      * @return The list of results for each callable task.
      */
     public List<V> invokeAll(List<Callable<V>> callableTasks, long timeout, TimeUnit unit) {
-        ExecutorService executor = Executors.newFixedThreadPool(mDevices.size());
+        ExecutorService executor =
+                Executors.newFixedThreadPool(
+                        mDevices.size(),
+                        new ThreadFactory() {
+                            @Override
+                            public Thread newThread(Runnable r) {
+                                Thread t = Executors.defaultThreadFactory().newThread(r);
+                                t.setDaemon(true);
+                                return t;
+                            }
+                        });
         List<V> results = new ArrayList<>();
         try {
             List<Future<V>> futures = executor.invokeAll(callableTasks);