• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28     private volatile static Throwable preallocatedException;
29 
createAndForget()30     static void createAndForget() {
31         Main main = new Main();
32     }
33 
main(String[] args)34     public static void main(String[] args) {
35         // 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 
40         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) {
56                 System.out.println(ie);
57             }
58         }
59 
60         /* give it a chance to cause mayhem */
61         try {
62             Thread.sleep(750);
63         } catch (InterruptedException ie) {
64             System.out.println(ie);
65         }
66 
67         System.out.println("done");
68     }
69 
finalize()70     protected void finalize() throws Throwable {
71         System.out.println("In finalizer");
72 
73         didFinal = true;
74 
75         throw preallocatedException;
76     }
77 }
78