blob: da5190a60629e103a5b66a573d62d611b5d8ecf1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2008 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
17import java.util.Timer;
18import java.util.TimerTask;
19
20/*
21 * Throw an exception from a finalizer and make sure it's harmless. Under
22 * Dalvik this may also generate a warning in the log file.
23 */
24public class Main {
25 static Object waiter = new Object();
26 static volatile boolean didFinal = false;
27
Andreas Gampe1dd05d72017-10-04 09:30:42 -070028 private volatile static Throwable preallocatedException;
29
jeffhao5d1ac922011-09-29 17:41:15 -070030 static void createAndForget() {
31 Main main = new Main();
32 }
33
34 public static void main(String[] args) {
Andreas Gampe1dd05d72017-10-04 09:30:42 -070035 // Preallocate exception to lighten the load in the time-sensitive section.
36 preallocatedException = new InterruptedException("whee");
37 // Print out something to avoid effects of being the first to write.
38 System.out.println("Starting");
39
jeffhao5d1ac922011-09-29 17:41:15 -070040 createAndForget();
41
42 System.gc();
43 System.runFinalization();
44
45 new Timer(true).schedule(new TimerTask() {
46 public void run() {
47 System.out.println("Timed out, exiting");
48 System.exit(1);
49 }
50 }, 30000);
51
52 while (!didFinal) {
53 try {
54 Thread.sleep(500);
55 } catch (InterruptedException ie) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000056 System.out.println(ie);
jeffhao5d1ac922011-09-29 17:41:15 -070057 }
58 }
59
60 /* give it a chance to cause mayhem */
61 try {
62 Thread.sleep(750);
63 } catch (InterruptedException ie) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000064 System.out.println(ie);
jeffhao5d1ac922011-09-29 17:41:15 -070065 }
66
67 System.out.println("done");
68 }
69
70 protected void finalize() throws Throwable {
71 System.out.println("In finalizer");
72
73 didFinal = true;
74
Andreas Gampe1dd05d72017-10-04 09:30:42 -070075 throw preallocatedException;
jeffhao5d1ac922011-09-29 17:41:15 -070076 }
77}