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