1 /*
   2  * Copyright (c) 2005, 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     6337571
  27  * @summary Test if findDeadlockedThreads works for an ownable synchronizer
  28  *          in shared mode which has no owner when a thread is parked.
  29  * @author  Mandy Chung
  30  *
  31  * @run main/othervm SharedSynchronizer
  32  */
  33 
  34 
  35 import java.util.concurrent.*;
  36 import java.lang.management.ManagementFactory;
  37 import java.lang.management.ThreadMXBean;
  38 
  39 public class SharedSynchronizer {
  40     public static void main(String[] args) throws Exception {
  41         MyThread t = new MyThread();
  42         t.setDaemon(true);
  43         t.start();
  44 
  45         ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
  46         if (!tmbean.isSynchronizerUsageSupported()) {
  47             System.out.println("Monitoring of synchronizer usage not supported")
  48 ;
  49             return;
  50         }
  51 
  52         long[] result = tmbean.findDeadlockedThreads();
  53         if (result != null) {
  54              throw new RuntimeException("TEST FAILED: result should be null");
  55         }
  56     }
  57 
  58     static class MyThread extends Thread {
  59         public void run() {
  60             FutureTask f = new FutureTask(
  61                 new Callable() {
  62                     public Object call() {
  63                         throw new RuntimeException("should never reach here");
  64                     }
  65                 }
  66             );
  67 
  68             // A FutureTask uses the AbstractOwnableSynchronizer in a shared
  69             // mode (not exclusive mode). When the thread calls f.get(),
  70             // it will put to park on the ownable synchronizer that
  71             // is not owned by any thread.
  72             try {
  73                 f.get();
  74             } catch (Exception e) {
  75                 RuntimeException re = new RuntimeException(e.getMessage());
  76                 re.initCause(e);
  77                 throw re;
  78             }
  79         }
  80     }
  81 }