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 } | 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.lang.management.ManagementFactory; 32 import java.lang.management.ThreadInfo; 33 import java.lang.management.ThreadMXBean; 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.atomic.AtomicInteger; 36 37 import static java.lang.annotation.RetentionPolicy.RUNTIME; 38 39 public 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 } |