LingeredAppWithDeadlock.java revision 2224:2a8815d86b93
1151912Sphk/* 2151912Sphk * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. 3209440Smav * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4151912Sphk * 5151912Sphk * This code is free software; you can redistribute it and/or modify it 6151912Sphk * under the terms of the GNU General Public License version 2 only, as 7151912Sphk * published by the Free Software Foundation. 8151912Sphk * 9151912Sphk * This code is distributed in the hope that it will be useful, but WITHOUT 10151912Sphk * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11151912Sphk * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12151912Sphk * version 2 for more details (a copy is included in the LICENSE file that 13151912Sphk * accompanied this code). 14151912Sphk * 15151912Sphk * You should have received a copy of the GNU General Public License version 16151912Sphk * 2 along with this work; if not, write to the Free Software Foundation, 17151912Sphk * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18151912Sphk * 19151912Sphk * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20151912Sphk * or visit www.oracle.com if you need additional information or have any 21151912Sphk * questions. 22151912Sphk */ 23151912Sphkpackage jdk.test.lib.apps; 24151912Sphk 25151912Sphkimport java.util.concurrent.Phaser; 26151912Sphk 27151912Sphkpublic class LingeredAppWithDeadlock extends LingeredApp { 28151912Sphk 29151912Sphk private static final Object Lock1 = new Object(); 30151912Sphk private static final Object Lock2 = new Object(); 31151912Sphk 32209402Smav private static volatile int reachCount = 0; 33209371Smav 34209371Smav private static final Phaser p = new Phaser(2); 35209371Smav 36209371Smav private static class ThreadOne extends Thread { 37151912Sphk public void run() { 38159217Snjl // wait Lock2 is locked 39151912Sphk p.arriveAndAwaitAdvance(); 40151912Sphk synchronized (Lock1) { 41209371Smav // signal Lock1 is locked 42151912Sphk p.arriveAndAwaitAdvance(); 43151912Sphk synchronized (Lock2) { 44209371Smav reachCount += 1; 45209371Smav } 46209371Smav } 47151912Sphk } 48159217Snjl } 49193530Sjkim 50193530Sjkim private static class ThreadTwo extends Thread { 51193530Sjkim public void run() { 52151912Sphk synchronized (Lock2) { 53175385Sjhb // signal Lock2 is locked 54151912Sphk p.arriveAndAwaitAdvance(); 55209371Smav // wait Lock1 is locked 56209371Smav p.arriveAndAwaitAdvance(); 57209371Smav synchronized (Lock1) { 58209371Smav reachCount += 1; 59203062Savg } 60203062Savg } 61203062Savg } 62151912Sphk } 63151912Sphk 64209371Smav public static void main(String args[]) { 65169574Stakawata if (args.length != 1) { 66151931Sscottl System.err.println("Lock file name is not specified"); 67151935Sscottl System.exit(7); 68151931Sscottl } 69151931Sscottl 70209371Smav // Run two theads that should come to deadlock 71151912Sphk new ThreadOne().start(); 72209371Smav new ThreadTwo().start(); 73209371Smav 74209371Smav if (reachCount > 0) { 75209371Smav // Not able to deadlock, exiting 76209440Smav System.exit(3); 77212238Smav } 78159217Snjl 79209371Smav LingeredApp.main(args); 80209371Smav } 81151912Sphk } 82209371Smav