blob: 3346aa681331ba7ff154d3c00c48d6580b379607 [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 Haoab820ee2016-06-15 16:20:34 -070039 try { barrier.await(); } catch (Exception e) { }
jeffhao5d1ac922011-09-29 17:41:15 -070040 try { Thread.sleep(6000); } catch (InterruptedException ie) { }
41
Elliott Hughes741b5b72012-01-31 19:18:51 -080042 System.out.println("Deadlock test interrupting threads.");
jeffhao5d1ac922011-09-29 17:41:15 -070043 thread1.interrupt();
44 thread2.interrupt();
45 System.out.println("Deadlock test main thread bailing.");
46 System.out.println("A initialized: " + aInitialized);
47 System.out.println("B initialized: " + bInitialized);
48 System.exit(0);
49 }
50}
51
52class A {
53 static {
Jeff Haoab820ee2016-06-15 16:20:34 -070054 try { Main.barrier.await(); } catch (Exception e) { }
jeffhao5d1ac922011-09-29 17:41:15 -070055 new B();
56 System.out.println("A initialized");
57 Main.aInitialized = true;
58 }
59}
60
61class B {
62 static {
Jeff Haoab820ee2016-06-15 16:20:34 -070063 try { Main.barrier.await(); } catch (Exception e) { }
jeffhao5d1ac922011-09-29 17:41:15 -070064 new A();
65 System.out.println("B initialized");
66 Main.bInitialized = true;
67 }
68}