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