1/* 2 * Copyright (c) 2013, 2015, 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 * @modules java.management 29 */ 30 31import java.lang.annotation.Retention; 32import java.lang.management.ManagementFactory; 33import java.lang.management.ThreadInfo; 34import java.lang.management.ThreadMXBean; 35import java.util.concurrent.CountDownLatch; 36import java.util.concurrent.atomic.AtomicInteger; 37 38import static java.lang.annotation.RetentionPolicy.RUNTIME; 39 40public class AnnotationTypeDeadlockTest { 41 42 @Retention(RUNTIME) 43 @AnnB 44 public @interface AnnA { 45 } 46 47 @Retention(RUNTIME) 48 @AnnA 49 public @interface AnnB { 50 } 51 52 static class Task extends Thread { 53 final CountDownLatch prepareLatch; 54 final AtomicInteger goLatch; 55 final Class<?> clazz; 56 57 Task(CountDownLatch prepareLatch, AtomicInteger goLatch, Class<?> clazz) { 58 super(clazz.getSimpleName()); 59 setDaemon(true); // in case it deadlocks 60 this.prepareLatch = prepareLatch; 61 this.goLatch = goLatch; 62 this.clazz = clazz; 63 } 64 65 @Override 66 public void run() { 67 prepareLatch.countDown(); // notify we are prepared 68 while (goLatch.get() > 0); // spin-wait before go 69 clazz.getDeclaredAnnotations(); 70 } 71 } 72 73 public static void main(String[] args) throws Exception { 74 CountDownLatch prepareLatch = new CountDownLatch(2); 75 AtomicInteger goLatch = new AtomicInteger(1); 76 Task taskA = new Task(prepareLatch, goLatch, AnnA.class); 77 Task taskB = new Task(prepareLatch, goLatch, AnnB.class); 78 taskA.start(); 79 taskB.start(); 80 // wait until both threads start-up 81 prepareLatch.await(); 82 // let them go 83 goLatch.set(0); 84 // obtain ThreadMXBean 85 ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); 86 // wait for threads to finish or dead-lock 87 while (taskA.isAlive() || taskB.isAlive()) { 88 // attempt to join threads 89 taskA.join(500L); 90 taskB.join(500L); 91 // detect dead-lock 92 long[] deadlockedIds = threadBean.findMonitorDeadlockedThreads(); 93 if (deadlockedIds != null && deadlockedIds.length > 0) { 94 StringBuilder sb = new StringBuilder("deadlock detected:\n\n"); 95 for (ThreadInfo ti : threadBean.getThreadInfo(deadlockedIds, Integer.MAX_VALUE)) { 96 sb.append(ti); 97 } 98 throw new IllegalStateException(sb.toString()); 99 } 100 } 101 } 102} 103