Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Test a class with a bad finalizer. |
| 19 | */ |
| 20 | public class Main { |
| 21 | public static void main(String[] args) { |
| 22 | BadFinalizer bf = new BadFinalizer(); |
| 23 | |
Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame^] | 24 | System.out.println("About to null reference and request GC."); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 25 | bf = null; |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 26 | Runtime.getRuntime().gc(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 27 | |
| 28 | for (int i = 0; i < 8; i++) { |
Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame^] | 29 | snooze(4000); |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 30 | Runtime.getRuntime().gc(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame^] | 33 | System.out.println("UNREACHABLE"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 34 | System.exit(0); |
| 35 | } |
Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame^] | 36 | |
| 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 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 67 | } |