1 /*
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  */
16 
17 import java.util.Timer;
18 import 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  */
24 public class Main {
25     static Object waiter = new Object();
26     static volatile boolean didFinal = false;
27 
createAndForget()28     static void createAndForget() {
29         Main main = new Main();
30     }
31 
main(String[] args)32     public static void main(String[] args) {
33         createAndForget();
34 
35         System.gc();
36         System.runFinalization();
37 
38         new Timer(true).schedule(new TimerTask() {
39                 public void run() {
40                     System.out.println("Timed out, exiting");
41                     System.exit(1);
42                 }
43             }, 30000);
44 
45         while (!didFinal) {
46             try {
47                 Thread.sleep(500);
48             } catch (InterruptedException ie) {
49                 System.err.println(ie);
50             }
51         }
52 
53         /* give it a chance to cause mayhem */
54         try {
55             Thread.sleep(750);
56         } catch (InterruptedException ie) {
57             System.err.println(ie);
58         }
59 
60         System.out.println("done");
61     }
62 
finalize()63     protected void finalize() throws Throwable {
64         System.out.println("In finalizer");
65 
66         didFinal = true;
67 
68         throw new InterruptedException("whee");
69     }
70 }
71