1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * Test a class with a bad finalizer.
19  */
20 public class Main {
main(String[] args)21     public static void main(String[] args) {
22         BadFinalizer bf = new BadFinalizer();
23 
24         System.out.println("About to null reference and request GC.");
25         bf = null;
26         Runtime.getRuntime().gc();
27 
28         for (int i = 0; i < 8; i++) {
29             snooze(4000);
30             Runtime.getRuntime().gc();
31         }
32 
33         System.out.println("UNREACHABLE");
34         System.exit(0);
35     }
36 
snooze(int ms)37     public static void snooze(int ms) {
38         try {
39             Thread.sleep(ms);
40         } catch (InterruptedException ie) {
41         }
42     }
43 
44     /**
45      * Class with a bad finalizer.
46      */
47     public static class BadFinalizer {
finalize()48         protected void finalize() {
49             System.out.println("Finalizer started and spinning...");
50             int j = 0;
51 
52             /* spin for a bit */
53             long start, end;
54             start = System.nanoTime();
55             for (int i = 0; i < 1000000; i++) {
56                 j++;
57             }
58             end = System.nanoTime();
59             System.out.println("Finalizer done spinning.");
60 
61             System.out.println("Finalizer sleeping forever now.");
62             while (true) {
63                 snooze(10000);
64             }
65         }
66     }
67 }
68