|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.concurrent.Callable; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.Future; |
| 31 | + |
| 32 | +import sun.jvmstat.monitor.MonitoredHost; |
| 33 | +import sun.jvmstat.monitor.VmIdentifier; |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @bug 8320687 |
| 38 | + * @summary verify that sun.jvmstat.monitor.MonitoredHost.getMonitoredHost() doesn't |
| 39 | + * unexpectedly throw an exception when invoked by concurrent threads |
| 40 | + * |
| 41 | + * @run main/othervm ConcurrentGetMonitoredHost |
| 42 | + */ |
| 43 | +public class ConcurrentGetMonitoredHost { |
| 44 | + |
| 45 | + /* |
| 46 | + * Launches multiple concurrent threads and invokes MonitoredHost.getMonitoredHost() |
| 47 | + * in each of these threads and expects the call to return successfully without any |
| 48 | + * exceptions. |
| 49 | + */ |
| 50 | + public static void main(final String[] args) throws Exception { |
| 51 | + final String pidStr = "12345"; |
| 52 | + final VmIdentifier vmid = new VmIdentifier(pidStr); |
| 53 | + final int numTasks = 100; |
| 54 | + final List<Task> tasks = new ArrayList<>(); |
| 55 | + for (int i = 0; i < numTasks; i++) { |
| 56 | + tasks.add(new Task(vmid)); |
| 57 | + } |
| 58 | + System.out.println("Submitting " + numTasks + " concurrent tasks to" + |
| 59 | + " get MonitoredHost for " + vmid); |
| 60 | + try { |
| 61 | + final ExecutorService executor = Executors.newCachedThreadPool(); |
| 62 | + // wait for all tasks to complete |
| 63 | + final List<Future<MonitoredHost>> results = executor.invokeAll(tasks); |
| 64 | + // verify each one successfully completed and each of |
| 65 | + // the returned MonitoredHost is not null |
| 66 | + for (final Future<MonitoredHost> result : results) { |
| 67 | + final MonitoredHost mh = result.get(); |
| 68 | + if (mh == null) { |
| 69 | + throw new AssertionError("MonitoredHost.getMonitoredHost() returned" + |
| 70 | + " null for vmid " + vmid); |
| 71 | + } |
| 72 | + } |
| 73 | + } catch (Exception e) { |
| 74 | + } |
| 75 | + System.out.println("All " + numTasks + " completed successfully"); |
| 76 | + } |
| 77 | + |
| 78 | + // a task which just calls MonitoredHost.getMonitoredHost(VmIdentifier) and |
| 79 | + // returns the resultant MonitoredHost |
| 80 | + private static final class Task implements Callable<MonitoredHost> { |
| 81 | + private final VmIdentifier vmid; |
| 82 | + |
| 83 | + private Task(final VmIdentifier vmid) { |
| 84 | + this.vmid = Objects.requireNonNull(vmid); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public MonitoredHost call() throws Exception { |
| 89 | + return MonitoredHost.getMonitoredHost(this.vmid); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments