Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
Jeff Hao | ab820ee | 2016-06-15 16:20:34 -0700 | [diff] [blame] | 17 | import java.util.concurrent.CyclicBarrier; |
| 18 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | |
Jeff Hao | ab820ee | 2016-06-15 16:20:34 -0700 | [diff] [blame] | 28 | public static CyclicBarrier barrier = new CyclicBarrier(3); |
| 29 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | |
Jeff Hao | d7ceb51 | 2016-06-16 11:29:42 -0700 | [diff] [blame] | 39 | // Not expecting any exceptions, so print them out if we get them. |
| 40 | try { barrier.await(); } catch (Exception e) { System.out.println(e); } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 41 | try { Thread.sleep(6000); } catch (InterruptedException ie) { } |
| 42 | |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 43 | System.out.println("Deadlock test interrupting threads."); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 { |
Jeff Hao | d7ceb51 | 2016-06-16 11:29:42 -0700 | [diff] [blame] | 55 | // Not expecting any exceptions, so print them out if we get them. |
| 56 | try { Main.barrier.await(); } catch (Exception e) { System.out.println(e); } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 57 | new B(); |
| 58 | System.out.println("A initialized"); |
| 59 | Main.aInitialized = true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | class B { |
| 64 | static { |
Jeff Hao | d7ceb51 | 2016-06-16 11:29:42 -0700 | [diff] [blame] | 65 | // Not expecting any exceptions, so print them out if we get them. |
| 66 | try { Main.barrier.await(); } catch (Exception e) { System.out.println(e); } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 67 | new A(); |
| 68 | System.out.println("B initialized"); |
| 69 | Main.bInitialized = true; |
| 70 | } |
| 71 | } |