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 import java.util.concurrent.CyclicBarrier;
18 
19 /**
20  * This causes most VMs to lock up.
21  *
22  * Interrupting threads in class initialization should NOT work.
23  */
24 public class Main {
25     public static boolean aInitialized = false;
26     public static boolean bInitialized = false;
27 
28     public static CyclicBarrier barrier = new CyclicBarrier(3);
29 
main(String[] args)30     static public void main(String[] args) {
31         Thread thread1, thread2;
32 
33         System.out.println("Deadlock test starting.");
34         thread1 = new Thread() { public void run() { new A(); } };
35         thread2 = new Thread() { public void run() { new B(); } };
36         thread1.start();
37         thread2.start();
38 
39         // Not expecting any exceptions, so print them out if we get them.
40         try { barrier.await(); } catch (Exception e) { System.out.println(e); }
41         try { Thread.sleep(6000); } catch (InterruptedException ie) { }
42 
43         System.out.println("Deadlock test interrupting threads.");
44         thread1.interrupt();
45         thread2.interrupt();
46         System.out.println("Deadlock test main thread bailing.");
47         System.out.println("A initialized: " + aInitialized);
48         System.out.println("B initialized: " + bInitialized);
49         System.exit(0);
50     }
51 }
52 
53 class A {
54     static {
55         // Not expecting any exceptions, so print them out if we get them.
Main.barrier.await()56         try { Main.barrier.await(); } catch (Exception e) { System.out.println(e); }
B()57         new B();
58         System.out.println("A initialized");
59         Main.aInitialized = true;
60     }
61 }
62 
63 class B {
64     static {
65         // Not expecting any exceptions, so print them out if we get them.
Main.barrier.await()66         try { Main.barrier.await(); } catch (Exception e) { System.out.println(e); }
A()67         new A();
68         System.out.println("B initialized");
69         Main.bInitialized = true;
70     }
71 }
72