blob: 942ee259008c59472672aff9d6e912420453bd4b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
17/**
18 * Test a class with a bad finalizer.
19 */
20public class Main {
21 public static void main(String[] args) {
22 BadFinalizer bf = new BadFinalizer();
23
Brian Carlstrombca77e62014-02-20 14:02:48 -080024 System.out.println("About to null reference and request GC.");
jeffhao5d1ac922011-09-29 17:41:15 -070025 bf = null;
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080026 Runtime.getRuntime().gc();
jeffhao5d1ac922011-09-29 17:41:15 -070027
28 for (int i = 0; i < 8; i++) {
Brian Carlstrombca77e62014-02-20 14:02:48 -080029 snooze(4000);
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080030 Runtime.getRuntime().gc();
jeffhao5d1ac922011-09-29 17:41:15 -070031 }
32
Brian Carlstrombca77e62014-02-20 14:02:48 -080033 System.out.println("UNREACHABLE");
jeffhao5d1ac922011-09-29 17:41:15 -070034 System.exit(0);
35 }
Brian Carlstrombca77e62014-02-20 14:02:48 -080036
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 {
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 }
jeffhao5d1ac922011-09-29 17:41:15 -070067}