1 /*
   2  * Copyright (c) 2011, 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 /* @test
  25  * @bug 7068328
  26  * @summary Test if getObjectName handles properly when called by
  27  *          multiple threads simultaneously. Run in othervm mode to
  28  *          make sure the object name is not initialized to begin with.
  29  * @modules java.management
  30  * @run main/othervm GetObjectName
  31  */
  32 
  33 import java.lang.management.BufferPoolMXBean;
  34 import java.lang.management.ManagementFactory;
  35 import java.lang.management.PlatformLoggingMXBean;
  36 import java.lang.management.PlatformManagedObject;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 import java.util.concurrent.ExecutorService;
  40 import java.util.concurrent.Executors;
  41 import java.util.concurrent.LinkedBlockingQueue;
  42 import java.util.concurrent.TimeUnit;
  43 
  44 public class GetObjectName {
  45     private static boolean failed = false;
  46     public static void main(String[] args) throws Exception {
  47         int tasks = 10000;
  48         ExecutorService executor = Executors.newFixedThreadPool(10);
  49         submitTasks(executor, tasks);
  50         executor.shutdown();
  51         executor.awaitTermination(10, TimeUnit.SECONDS);
  52         if (!failed) {
  53             System.out.println("Test passed.");
  54         }
  55     }
  56 
  57     static void submitTasks(ExecutorService executor, int count) {
  58         for (int i=0; i < count && !failed; i++) {
  59             executor.execute(new Runnable() {
  60                 @Override
  61                 public void run() {
  62                     List<PlatformManagedObject> mbeans = new ArrayList<>();
  63                     mbeans.add(ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class));
  64                     mbeans.addAll(ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class));
  65                     for (PlatformManagedObject pmo : mbeans) {
  66                         // Name should not be null
  67                         if (pmo.getObjectName() == null) {
  68                             failed = true;
  69                             throw new RuntimeException("TEST FAILED: getObjectName() returns null");
  70                         }
  71                     }
  72                 }
  73             });
  74         }
  75     }
  76 }