blob: bd4d4ab7b5de6e688046c68c173910a56c44e41e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
Jeff Haoab820ee2016-06-15 16:20:34 -070017import java.util.concurrent.CyclicBarrier;
18
jeffhao5d1ac922011-09-29 17:41:15 -070019/**
20 * This causes most VMs to lock up.
21 *
22 * Interrupting threads in class initialization should NOT work.
23 */
24public class Main {
25 public static boolean aInitialized = false;
26 public static boolean bInitialized = false;
27
Jeff Haoab820ee2016-06-15 16:20:34 -070028 public static CyclicBarrier barrier = new CyclicBarrier(3);
29
jeffhao5d1ac922011-09-29 17:41:15 -070030 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
Jeff Haod7ceb512016-06-16 11:29:42 -070039 // Not expecting any exceptions, so print them out if we get them.
40 try { barrier.await(); } catch (Exception e) { System.out.println(e); }
jeffhao5d1ac922011-09-29 17:41:15 -070041 try { Thread.sleep(6000); } catch (InterruptedException ie) { }
42
Elliott Hughes741b5b72012-01-31 19:18:51 -080043 System.out.println("Deadlock test interrupting threads.");
jeffhao5d1ac922011-09-29 17:41:15 -070044 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
53class A {
54 static {
Jeff Haod7ceb512016-06-16 11:29:42 -070055 // Not expecting any exceptions, so print them out if we get them.
56 try { Main.barrier.await(); } catch (Exception e) { System.out.println(e); }
jeffhao5d1ac922011-09-29 17:41:15 -070057 new B();
58 System.out.println("A initialized");
59 Main.aInitialized = true;
60 }
61}
62
63class B {
64 static {
Jeff Haod7ceb512016-06-16 11:29:42 -070065 // Not expecting any exceptions, so print them out if we get them.
66 try { Main.barrier.await(); } catch (Exception e) { System.out.println(e); }
jeffhao5d1ac922011-09-29 17:41:15 -070067 new A();
68 System.out.println("B initialized");
69 Main.bInitialized = true;
70 }
71}