OSDN Git Service

Handle OOME during threadstress finishing message
authorMathieu Chartier <mathieuc@google.com>
Fri, 18 Sep 2015 03:46:56 +0000 (20:46 -0700)
committerMathieu Chartier <mathieuc@google.com>
Fri, 18 Sep 2015 17:59:39 +0000 (10:59 -0700)
If there is an OOME that occurs after the "Finishing workers" it
caused the test to fail.

Also guard worker thread creation by try catch for OOME.

Bug: 18577101
Change-Id: I69367be0aad3f60093c02c7f63ae3c20757fb89b

test/004-ThreadStress/src/Main.java

index 4eeae2f..9461c0b 100644 (file)
@@ -443,13 +443,14 @@ public class Main implements Runnable {
                     int id = threadStress.id;
                     System.out.println("Starting worker for " + id);
                     while (threadStress.nextOperation < operationsPerThread) {
-                        Thread thread = new Thread(ts, "Worker thread " + id);
-                        thread.start();
-                        try {
-                            thread.join();
-                        } catch (InterruptedException e) {
-                        }
                         try {
+                            Thread thread = new Thread(ts, "Worker thread " + id);
+                            thread.start();
+                            try {
+                                thread.join();
+                            } catch (InterruptedException e) {
+                            }
+
                             System.out.println("Thread exited for " + id + " with "
                                                + (operationsPerThread - threadStress.nextOperation)
                                                + " operations remaining.");
@@ -458,7 +459,14 @@ public class Main implements Runnable {
                             // to pass.
                         }
                     }
-                    System.out.println("Finishing worker");
+                    // Keep trying to print "Finishing worker" until it succeeds.
+                    while (true) {
+                        try {
+                            System.out.println("Finishing worker");
+                            break;
+                        } catch (OutOfMemoryError e) {
+                        }
+                    }
                 }
             };
         }