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.atomic.AtomicInteger; 32 33 import static java.lang.annotation.RetentionPolicy.RUNTIME; 34 35 public class AnnotationTypeDeadlockTest { 36 37 @Retention(RUNTIME) 38 @AnnB 39 public @interface AnnA { 40 } 41 42 @Retention(RUNTIME) 43 @AnnA 44 public @interface AnnB { 45 } 46 47 static class Task extends Thread { 48 final AtomicInteger latch; 49 final Class<?> clazz; 50 51 Task(AtomicInteger latch, Class<?> clazz) { 52 super(clazz.getSimpleName()); 53 setDaemon(true); // in case it deadlocks 54 this.latch = latch; 55 this.clazz = clazz; 56 } 57 58 @Override 59 public void run() { 60 latch.incrementAndGet(); 61 while (latch.get() > 0) ; // spin-wait 62 clazz.getDeclaredAnnotations(); 63 } 64 } 65 66 static void dumpState(Task task) { 67 System.err.println( 68 "Task[" + task.getName() + "].state: " + 69 task.getState() + " ..." 70 ); 71 for (StackTraceElement ste : task.getStackTrace()) { 72 System.err.println("\tat " + ste); 73 } 74 System.err.println(); 75 } 76 77 public static void main(String[] args) throws Exception { 78 AtomicInteger latch = new AtomicInteger(); 79 Task taskA = new Task(latch, AnnA.class); 80 Task taskB = new Task(latch, AnnB.class); 81 taskA.start(); 82 taskB.start(); 83 // spin-wait for both threads to start-up 84 while (latch.get() < 2) ; 85 // trigger coherent start 86 latch.set(0); 87 // join them 88 taskA.join(500L); 89 taskB.join(500L); 90 91 if (taskA.isAlive() || taskB.isAlive()) { 92 dumpState(taskA); 93 dumpState(taskB); 94 throw new IllegalStateException( 95 taskA.getState() == Thread.State.BLOCKED && 96 taskB.getState() == Thread.State.BLOCKED 97 ? "deadlock detected" 98 : "unexpected condition"); 99 } 100 } 101 }