1 /* 2 * Copyright (c) 2013, 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 24 /* 25 * @test 26 * @bug 7122142 27 * @summary Test deadlock situation when recursive annotations are parsed 28 */ 29 30 import java.lang.annotation.Retention; 31 import java.util.concurrent.CountDownLatch; 32 import java.util.concurrent.atomic.AtomicInteger; 33 34 import static java.lang.annotation.RetentionPolicy.RUNTIME; 35 36 public class AnnotationTypeDeadlockTest { 37 38 @Retention(RUNTIME) 39 @AnnB 40 public @interface AnnA { 41 } 42 43 @Retention(RUNTIME) 44 @AnnA 45 public @interface AnnB { 46 } 47 48 static class Task extends Thread { 49 final CountDownLatch prepareLatch; 50 final AtomicInteger goLatch; 51 final Class<?> clazz; 52 53 Task(CountDownLatch prepareLatch, AtomicInteger goLatch, Class<?> clazz) { 54 super(clazz.getSimpleName()); 55 setDaemon(true); // in case it deadlocks 56 this.prepareLatch = prepareLatch; 57 this.goLatch = goLatch; 58 this.clazz = clazz; 59 } 60 61 @Override 62 public void run() { 63 prepareLatch.countDown(); // notify we are prepared 64 while (goLatch.get() > 0); // spin-wait before go 65 clazz.getDeclaredAnnotations(); 66 } 67 } 68 69 static void dumpState(Task task) { 70 System.err.println( 71 "Task[" + task.getName() + "].state: " + 72 task.getState() + " ..." 73 ); 74 for (StackTraceElement ste : task.getStackTrace()) { 75 System.err.println("\tat " + ste); 76 } 77 System.err.println(); 78 } 79 80 public static void main(String[] args) throws Exception { 81 CountDownLatch prepareLatch = new CountDownLatch(2); 82 AtomicInteger goLatch = new AtomicInteger(1); 83 Task taskA = new Task(prepareLatch, goLatch, AnnA.class); 84 Task taskB = new Task(prepareLatch, goLatch, AnnB.class); 85 taskA.start(); 86 taskB.start(); 87 // wait until both threads start-up 88 prepareLatch.await(); 89 // let them go 90 goLatch.set(0); 91 // attempt to join them 92 taskA.join(5000L); 93 taskB.join(5000L); 94 95 if (taskA.isAlive() || taskB.isAlive()) { 96 dumpState(taskA); 97 dumpState(taskB); 98 throw new IllegalStateException( 99 taskA.getState() == Thread.State.BLOCKED && 100 taskB.getState() == Thread.State.BLOCKED 101 ? "deadlock detected" 102 : "unexpected condition"); 103 } 104 } 105 }