1 /* 2 * Copyright 2014 SAP AG. 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 24 public class RecursiveObjectLock { 25 26 public ALock lightLock = new ALock(); 27 28 29 public void testMethod() { 30 ALock lock = lightLock; 31 synchronized (lock) { 32 nestedLock1(lock); 33 } 34 } 35 36 public void nestedLock1(ALock lock) { 37 synchronized (lock) { 38 nestedLock2(lock); 39 } 40 } 41 42 public void nestedLock2(ALock lock) { 43 synchronized (lock) { 44 callWait(lock); 45 } 46 } 47 48 public void callWait(ALock lock){ 49 try { 50 lock.wait(1); 51 } catch (InterruptedException e) { 52 53 e.printStackTrace(); 54 } 55 breakpoint1(); 56 } 57 58 public static void breakpoint1() { 59 // purpose: hold a breakpoint 60 } 61 62 public static void main(String[] args) { 63 RecursiveObjectLock ro = new RecursiveObjectLock(); 64 ro.testMethod(); 65 System.out.println("ready"); 66 } 67 68 static class ALock { 69 // rather use this type for locking tests, because Object could be excluded from biased locking 70 } 71 }