Path: blob/master/test/jdk/java/lang/management/ManagementFactory/GetObjectName.java
41152 views
/*1* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 706832825* @summary Test if getObjectName handles properly when called by26* multiple threads simultaneously. Run in othervm mode to27* make sure the object name is not initialized to begin with.28*29* @run main/othervm GetObjectName30*/3132import java.lang.management.BufferPoolMXBean;33import java.lang.management.ManagementFactory;34import java.lang.management.PlatformLoggingMXBean;35import java.lang.management.PlatformManagedObject;36import java.util.ArrayList;37import java.util.List;38import java.util.concurrent.ExecutorService;39import java.util.concurrent.Executors;40import java.util.concurrent.LinkedBlockingQueue;41import java.util.concurrent.TimeUnit;4243public class GetObjectName {44private static boolean failed = false;45public static void main(String[] args) throws Exception {46int tasks = 10000;47ExecutorService executor = Executors.newFixedThreadPool(10);48submitTasks(executor, tasks);49executor.shutdown();50executor.awaitTermination(10, TimeUnit.SECONDS);51if (!failed) {52System.out.println("Test passed.");53}54}5556static void submitTasks(ExecutorService executor, int count) {57for (int i=0; i < count && !failed; i++) {58executor.execute(new Runnable() {59@Override60public void run() {61List<PlatformManagedObject> mbeans = new ArrayList<>();62mbeans.add(ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class));63mbeans.addAll(ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class));64for (PlatformManagedObject pmo : mbeans) {65// Name should not be null66if (pmo.getObjectName() == null) {67failed = true;68throw new RuntimeException("TEST FAILED: getObjectName() returns null");69}70}71}72});73}74}75}767778