• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 /**
18  * This causes most VMs to lock up.
19  *
20  * Interrupting threads in class initialization should NOT work.
21  */
22 public class Main {
23     public static boolean aInitialized = false;
24     public static boolean bInitialized = false;
25 
main(String[] args)26     static public void main(String[] args) {
27         Thread thread1, thread2;
28 
29         System.out.println("Deadlock test starting.");
30         thread1 = new Thread() { public void run() { new A(); } };
31         thread2 = new Thread() { public void run() { new B(); } };
32         thread1.start();
33         // Give thread1 a chance to start before starting thread2.
34         try { Thread.sleep(1000); } catch (InterruptedException ie) { }
35         thread2.start();
36 
37         try { Thread.sleep(6000); } catch (InterruptedException ie) { }
38 
39         System.out.println("Deadlock test interrupting threads.");
40         thread1.interrupt();
41         thread2.interrupt();
42         System.out.println("Deadlock test main thread bailing.");
43         System.out.println("A initialized: " + aInitialized);
44         System.out.println("B initialized: " + bInitialized);
45         System.exit(0);
46     }
47 }
48 
49 class A {
50     static {
51         System.out.println("A initializing...");
52         try { Thread.sleep(3000); } catch (InterruptedException ie) { }
B()53         new B();
54         System.out.println("A initialized");
55         Main.aInitialized = true;
56     }
57 }
58 
59 class B {
60     static {
61         System.out.println("B initializing...");
62         try { Thread.sleep(3000); } catch (InterruptedException ie) { }
A()63         new A();
64         System.out.println("B initialized");
65         Main.bInitialized = true;
66     }
67 }
68