Try to make 036-finalizer less flaky by moving output to one thread

Bug: 13108062
Change-Id: I946044d007dda4efa85794e2361ecb2ce102be64
diff --git a/test/036-finalizer/expected.txt b/test/036-finalizer/expected.txt
index f9b29b0..a2a74fc 100644
--- a/test/036-finalizer/expected.txt
+++ b/test/036-finalizer/expected.txt
@@ -1,14 +1,13 @@
-wimp: wahoo
+wimp: [FinalizerTest message=wahoo, finalized=false]
 gc
-finalizer executed: wahoo
 wimp: null
 finalize
 wimp: null
 sleep
-reborn: wahoo
+reborn: [FinalizerTest message=wahoo, finalized=true]
 wimp: null
 reset reborn
 gc + finalize
 sleep
-reborn: nothing
+reborn: [FinalizerTest message=nothing, finalized=false]
 wimp: null
diff --git a/test/036-finalizer/src/FinalizerTest.java b/test/036-finalizer/src/FinalizerTest.java
deleted file mode 100644
index b0d014d..0000000
--- a/test/036-finalizer/src/FinalizerTest.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.lang.ref.WeakReference;
-
-public class FinalizerTest {
-    public static FinalizerTest mNothing = new FinalizerTest("nothing");
-    public static FinalizerTest mReborn = mNothing;
-
-    public String mMsg = "default";
-
-    public FinalizerTest(String msg) {
-        mMsg = msg;
-    }
-
-    public String toString() {
-        return mMsg;
-    }
-
-    protected void finalize() {
-        System.out.println("finalizer executed: " + mMsg);
-        mReborn = this;
-    }
-}
diff --git a/test/036-finalizer/src/Main.java b/test/036-finalizer/src/Main.java
index 6195aff..328425f 100644
--- a/test/036-finalizer/src/Main.java
+++ b/test/036-finalizer/src/Main.java
@@ -15,6 +15,8 @@
  */
 
 import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Some finalizer tests.
@@ -31,18 +33,19 @@
         }
     }
 
-    public static WeakReference makeRef() {
+    public static WeakReference<FinalizerTest> makeRef() {
         /*
          * Make ft in another thread, so there is no danger of
          * a conservative reference leaking onto the main thread's
          * stack.
          */
 
-        final WeakReference[] wimp = new WeakReference[1];
+        final List<WeakReference<FinalizerTest>> wimp =
+                new ArrayList<WeakReference<FinalizerTest>>();
         Thread t = new Thread() {
                 public void run() {
                     FinalizerTest ft = new FinalizerTest("wahoo");
-                    wimp[0] = new WeakReference(ft);
+                    wimp.add(new WeakReference<FinalizerTest>(ft));
                     ft = null;
                 }
             };
@@ -55,10 +58,10 @@
             throw new RuntimeException(ie);
         }
 
-        return wimp[0];
+        return wimp.get(0);
     }
 
-    public static String wimpString(final WeakReference wimp) {
+    public static String wimpString(final WeakReference<FinalizerTest> wimp) {
         /*
          * Do the work in another thread, so there is no danger of a
          * conservative reference to ft leaking onto the main thread's
@@ -68,7 +71,7 @@
         final String[] s = new String[1];
         Thread t = new Thread() {
                 public void run() {
-                    Object ref = wimp.get();
+                    FinalizerTest ref = wimp.get();
                     if (ref != null) {
                         s[0] = ref.toString();
                     }
@@ -87,7 +90,7 @@
     }
 
     public static void main(String[] args) {
-        WeakReference wimp = makeRef();
+        WeakReference<FinalizerTest> wimp = makeRef();
 
         System.out.println("wimp: " + wimpString(wimp));
 
@@ -118,4 +121,26 @@
         System.out.println("reborn: " + FinalizerTest.mReborn);
         System.out.println("wimp: " + wimpString(wimp));
     }
+
+    public static class FinalizerTest {
+        public static FinalizerTest mNothing = new FinalizerTest("nothing");
+        public static FinalizerTest mReborn = mNothing;
+
+        private final String message;
+        private boolean finalized = false;
+
+        public FinalizerTest(String message) {
+            this.message = message;
+        }
+
+        public String toString() {
+            return "[FinalizerTest message=" + message +
+                    ", finalized=" + finalized + "]";
+        }
+
+        protected void finalize() {
+            finalized = true;
+            mReborn = this;
+        }
+    }
 }