1 /*
   2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.test.lib.apps;
  24 
  25 import java.util.concurrent.Phaser;
  26 
  27 public class LingeredAppWithDeadlock extends LingeredApp {
  28 
  29     private static final Object Lock1 = new Object();
  30     private static final Object Lock2 = new Object();
  31 
  32     private static volatile int reachCount = 0;
  33 
  34     private static final Phaser p = new Phaser(2);
  35 
  36     private static class ThreadOne extends Thread {
  37         public void run() {
  38             // wait Lock2 is locked
  39             p.arriveAndAwaitAdvance();
  40             synchronized (Lock1) {
  41                 // signal Lock1 is locked
  42                 p.arriveAndAwaitAdvance();
  43                 synchronized (Lock2) {
  44                     reachCount += 1;
  45                 }
  46             }
  47         }
  48     }
  49 
  50     private static class ThreadTwo extends Thread {
  51         public void run() {
  52             synchronized (Lock2) {
  53                 // signal Lock2 is locked
  54                 p.arriveAndAwaitAdvance();
  55                 // wait Lock1 is locked
  56                 p.arriveAndAwaitAdvance();
  57                 synchronized (Lock1) {
  58                     reachCount += 1;
  59                 }
  60             }
  61         }
  62     }
  63 
  64     public static void main(String args[]) {
  65         if (args.length != 1) {
  66             System.err.println("Lock file name is not specified");
  67             System.exit(7);
  68         }
  69 
  70         // Run two theads that should come to deadlock
  71         new ThreadOne().start();
  72         new ThreadTwo().start();
  73 
  74         if (reachCount > 0) {
  75             // Not able to deadlock, exiting
  76             System.exit(3);
  77         }
  78 
  79         LingeredApp.main(args);
  80     }
  81  }