changeset 52284:69ee8894f5fa

8210022: remove jdk.testlibrary.ProcessThread, TestThread and XRun Reviewed-by: sspitsyn, jcbeyler
author iignatyev
date Mon, 27 Aug 2018 21:50:52 -0700
parents 3d3e8a33701e
children 04b857edadec
files test/jdk/com/sun/tools/attach/BasicTests.java test/jdk/com/sun/tools/attach/PermissionTest.java test/jdk/com/sun/tools/attach/ProviderTest.java test/jdk/com/sun/tools/attach/RunnerUtil.java test/jdk/com/sun/tools/attach/StartManagementAgent.java test/jdk/com/sun/tools/attach/TempDirTest.java test/jdk/lib/testlibrary/jdk/testlibrary/ProcessThread.java test/jdk/lib/testlibrary/jdk/testlibrary/TestThread.java test/jdk/lib/testlibrary/jdk/testlibrary/XRun.java test/jdk/sun/management/jmxremote/bootstrap/JMXInterfaceBindingTest.java test/jdk/sun/tools/jstatd/JstatdTest.java test/jdk/sun/tools/jstatd/TestJstatdDefaults.java test/jdk/sun/tools/jstatd/TestJstatdExternalRegistry.java test/jdk/sun/tools/jstatd/TestJstatdPort.java test/jdk/sun/tools/jstatd/TestJstatdPortAndServer.java test/jdk/sun/tools/jstatd/TestJstatdServer.java test/jdk/sun/tools/jstatd/TestJstatdUsage.java test/lib/jdk/test/lib/thread/ProcessThread.java
diffstat 18 files changed, 249 insertions(+), 494 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/com/sun/tools/attach/BasicTests.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/com/sun/tools/attach/BasicTests.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,8 +28,8 @@
 import java.util.List;
 import java.util.Properties;
 
+import jdk.test.lib.thread.ProcessThread;
 import jdk.testlibrary.OutputAnalyzer;
-import jdk.testlibrary.ProcessThread;
 import jdk.testlibrary.ProcessTools;
 
 import com.sun.tools.attach.AgentInitializationException;
@@ -45,6 +45,7 @@
  * a number of basic attach tests.
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules java.instrument
  *          jdk.attach
  *          jdk.jartool/sun.tools.jar
--- a/test/jdk/com/sun/tools/attach/PermissionTest.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/com/sun/tools/attach/PermissionTest.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,9 +24,9 @@
 import com.sun.tools.attach.VirtualMachine;
 import com.sun.tools.attach.AttachNotSupportedException;
 import java.io.File;
+import jdk.test.lib.thread.ProcessThread;
 import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.ProcessTools;
-import jdk.testlibrary.ProcessThread;
 
 /*
  * @test
@@ -35,6 +35,7 @@
  * API - this checks that a SecurityException is thrown as expected.
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules jdk.attach
  *          jdk.jartool/sun.tools.jar
  *
--- a/test/jdk/com/sun/tools/attach/ProviderTest.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/com/sun/tools/attach/ProviderTest.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
  * and detach to/from the running Application.
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules jdk.attach
  *          jdk.jartool/sun.tools.jar
  *
--- a/test/jdk/com/sun/tools/attach/RunnerUtil.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/com/sun/tools/attach/RunnerUtil.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,9 +26,9 @@
 import java.nio.file.Files;
 import java.util.Arrays;
 
+import jdk.test.lib.thread.ProcessThread;
 import jdk.testlibrary.ProcessTools;
 import jdk.testlibrary.Utils;
-import jdk.testlibrary.ProcessThread;
 
 /*
  * Utility functions for test runners.
--- a/test/jdk/com/sun/tools/attach/StartManagementAgent.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/com/sun/tools/attach/StartManagementAgent.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,7 @@
 import javax.management.remote.JMXConnector;
 import javax.management.remote.JMXConnectorFactory;
 
-import jdk.testlibrary.ProcessThread;
+import jdk.test.lib.thread.ProcessThread;
 import jdk.testlibrary.Utils;
 
 /*
@@ -41,6 +41,7 @@
  * @summary Test for VirtualMachine.startManagementAgent and VirtualMachine.startLocalManagementAgent
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules java.management
  *          jdk.attach
  *          jdk.jartool/sun.tools.jar
--- a/test/jdk/com/sun/tools/attach/TempDirTest.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/com/sun/tools/attach/TempDirTest.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,9 +29,9 @@
 import java.util.List;
 import java.io.File;
 
+import jdk.test.lib.thread.ProcessThread;
 import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.ProcessTools;
-import jdk.testlibrary.ProcessThread;
 
 /*
  * @test
@@ -39,6 +39,7 @@
  * @summary Test to make sure attach and jvmstat works correctly when java.io.tmpdir is set
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules jdk.attach
  *          jdk.jartool/sun.tools.jar
  *
--- a/test/jdk/lib/testlibrary/jdk/testlibrary/ProcessThread.java	Mon Aug 27 17:01:15 2018 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,213 +0,0 @@
-/*
- * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.testlibrary;
-
-import java.io.PrintWriter;
-
-import java.util.concurrent.CountDownLatch;
-import java.util.function.Predicate;
-
-/**
- * The helper class for starting and stopping {@link Process} in a separate thread.
- */
-public class ProcessThread extends TestThread {
-
-    /**
-     * Creates a new {@code ProcessThread} object.
-     *
-     * @param threadName The name of thread
-     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
-     */
-    public ProcessThread(String threadName, String... cmd) {
-        super(new ProcessRunnable(new ProcessBuilder(cmd)), threadName);
-    }
-
-    /**
-     * Creates a new {@code ProcessThread} object.
-     *
-     * @param threadName The name of thread.
-     * @param pb The ProcessBuilder to execute.
-     */
-    public ProcessThread(String threadName, ProcessBuilder pb) {
-        super(new ProcessRunnable(pb), threadName);
-    }
-
-
-    /**
-     * Creates a new {@code ProcessThread} object.
-     *
-     * @param threadName The name of thread
-     * @param waitfor A predicate to determine whether the target process has been initialized
-     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
-     */
-    public ProcessThread(String threadName, Predicate<String> waitfor, String... cmd) {
-        super(new ProcessRunnable(new ProcessBuilder(cmd), threadName, waitfor), threadName);
-    }
-
-    /**
-     * Creates a new {@code ProcessThread} object.
-     *
-     * @param threadName The name of thread.
-     * @param waitfor A predicate to determine whether the target process has been initialized
-     * @param pb The ProcessBuilder to execute.
-     */
-    public ProcessThread(String threadName, Predicate<String> waitfor, ProcessBuilder pb) {
-        super(new ProcessRunnable(pb, threadName, waitfor), threadName);
-    }
-
-    /**
-     * Stops {@link Process} started by {@code ProcessRunnable}.
-     *
-     * @throws InterruptedException
-     */
-    public void stopProcess() throws InterruptedException {
-        ((ProcessRunnable) getRunnable()).stopProcess();
-    }
-
-    /**
-     * @return The process output, or null if the process has not yet completed.
-     */
-    public OutputAnalyzer getOutput() {
-        return ((ProcessRunnable) getRunnable()).getOutput();
-    }
-
-    /**
-    * Returns the PID associated with this process thread
-    * @return The PID associated with this process thread
-    */
-    public long getPid() throws InterruptedException {
-        return ((ProcessRunnable)getRunnable()).getPid();
-    }
-
-    public void sendMessage(String message) throws InterruptedException {
-        ((ProcessRunnable)getRunnable()).sendMessage(message);
-    }
-
-    /**
-     * {@link Runnable} interface for starting and stopping {@link Process}.
-     */
-    static class ProcessRunnable extends XRun {
-
-        private final ProcessBuilder processBuilder;
-        private final CountDownLatch latch;
-        private volatile Process process;
-        private volatile OutputAnalyzer output;
-        private final Predicate<String> waitfor;
-        private final String name;
-
-        /**
-         * Creates a new {@code ProcessRunnable} object.
-         *
-         * @param pb The {@link ProcessBuilder} to run.
-         */
-        public ProcessRunnable(ProcessBuilder pb) {
-            this(pb, "", null);
-        }
-
-        /**
-         * Creates a new {@code ProcessRunnable} object.
-         *
-         * @param pb The {@link ProcessBuilder} to run.
-         * @param name An optional process name; may be null
-         * @param waitfor A predicate to determine whether the target process has been initialized; may be null
-         */
-        public ProcessRunnable(ProcessBuilder pb, String name, Predicate<String> waitfor) {
-            this.processBuilder = pb;
-            this.latch = new CountDownLatch(1);
-            this.name = name;
-            this.waitfor = waitfor;
-        }
-
-        /**
-         * Starts the process in {@code ProcessThread}.
-         * All exceptions which occurs here will be caught and stored in {@code ProcessThread}.
-         *
-         * see {@link XRun}
-         */
-        @Override
-        public void xrun() throws Throwable {
-            this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
-            // Release when process is started
-            latch.countDown();
-
-            // Will block...
-            try {
-                this.process.waitFor();
-                output = new OutputAnalyzer(this.process);
-            } catch (Throwable t) {
-                String name = Thread.currentThread().getName();
-                System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
-                throw t;
-            } finally {
-                this.process.destroyForcibly().waitFor();
-                String logMsg = ProcessTools.getProcessLog(processBuilder, output);
-                System.out.println(logMsg);
-            }
-        }
-
-        /**
-         * Stops the process.
-         *
-         * @throws InterruptedException
-         */
-        public void stopProcess() throws InterruptedException {
-            // Wait until process is started
-            latch.await();
-            if (this.process != null) {
-                System.out.println("ProcessThread.stopProcess() will kill process");
-                this.process.destroy();
-            }
-        }
-
-        /**
-         * Returns the OutputAnalyzer with stdout/stderr from the process.
-         * @return The process output, or null if process not completed.
-         * @throws InterruptedException
-         */
-        public OutputAnalyzer getOutput() {
-            return output;
-        }
-
-        /**
-         * Returns the PID associated with this process runnable
-         * @return The PID associated with this process runnable
-         */
-        public long getPid() throws InterruptedException {
-            return getProcess().pid();
-        }
-
-        public void sendMessage(String message) throws InterruptedException {
-            try (PrintWriter pw = new PrintWriter(this.getProcess().getOutputStream())) {
-                pw.println(message);
-                pw.flush();
-            }
-        }
-
-        private Process getProcess() throws InterruptedException {
-            latch.await();
-            return process;
-        }
-    }
-
-}
--- a/test/jdk/lib/testlibrary/jdk/testlibrary/TestThread.java	Mon Aug 27 17:01:15 2018 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,204 +0,0 @@
-/*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.testlibrary;
-
-import java.util.concurrent.TimeoutException;
-
-/**
- * Thread which catches exceptions thrown during the execution
- * and stores them for later analysis.
- *
- * <pre>
- * {@code
- * TestThread thread = new TestThread(new XRun() {
- *      public void run() {
- *      // do something
- *      }
- * });
- * thread.start();
- * // do something
- * Throwable uncaught = thread.getUncaught();
- * }
- * </pre>
- */
-public class TestThread extends Thread {
-
-    private final Runnable runnable;
-    private volatile Throwable uncaught;
-
-    /**
-     * Returns {@link Runnable} the thread has been created with.
-     *
-     * @return The object whose {@code run} method is called
-     */
-    public Runnable getRunnable() {
-        return runnable;
-    }
-
-    /**
-     * Creates a new {@code TestThread} object.
-     *
-     * @param target The object whose {@code run} method is called
-     * @param name The thread name
-     */
-    public TestThread(Runnable target, String name) {
-        super(target, name);
-        this.runnable = target;
-    }
-
-    /**
-     * Creates a new {@code TestThread} object.
-     *
-     * @param target The object whose {@code run} method is called
-     */
-    public TestThread(Runnable target) {
-        super(target);
-        this.runnable = target;
-    }
-
-    /**
-     * Creates a new {@code TestThread} object.
-     *
-     * @param group The thread group
-     * @param target The object whose {@code run} method is called
-     * @param name The thread name
-     * @param stackSize Stack size
-     */
-    public TestThread(ThreadGroup group, Runnable target, String name,
-            long stackSize) {
-        super(group, target, name, stackSize);
-        this.runnable = target;
-    }
-
-    /**
-     * Creates a new {@code TestThread} object.
-     *
-     * @param group The thread group
-     * @param target The object whose {@code run} method is called
-     * @param name The thread name
-     */
-    public TestThread(ThreadGroup group, Runnable target, String name) {
-        super(group, target, name);
-        this.runnable = target;
-    }
-
-    /**
-     * Creates a new {@code TestThread} object.
-     *
-     * @param group The thread group
-     * @param target The object whose {@code run} method is called
-     */
-    public TestThread(ThreadGroup group, Runnable target) {
-        super(group, target);
-        this.runnable = target;
-    }
-
-    /**
-     * The thread executor.
-     */
-    @Override
-    public void run() {
-        try {
-            super.run();
-        } catch (Throwable t) {
-            uncaught = t;
-        }
-    }
-
-    /**
-     * Returns exception caught during the execution.
-     *
-     * @return {@link Throwable}
-     */
-    public Throwable getUncaught() {
-        return uncaught;
-    }
-
-    /**
-     * Waits for {@link TestThread} to die
-     * and throws exception caught during the execution.
-     *
-     * @throws InterruptedException
-     * @throws Throwable
-     */
-    public void joinAndThrow() throws InterruptedException, Throwable {
-        join();
-        if (uncaught != null) {
-            throw uncaught;
-        }
-    }
-
-    /**
-     * Waits during {@code timeout} for {@link TestThread} to die
-     * and throws exception caught during the execution.
-     *
-     * @param timeout The time to wait in milliseconds
-     * @throws InterruptedException
-     * @throws Throwable
-     */
-    public void joinAndThrow(long timeout) throws InterruptedException,
-            Throwable {
-        join(timeout);
-        if (isAlive()) {
-            throw new TimeoutException();
-        }
-        if (uncaught != null) {
-            throw uncaught;
-        }
-    }
-
-    /**
-     * Waits for {@link TestThread} to die
-     * and returns exception caught during the execution.
-     *
-     * @return Exception caught during the execution
-     * @throws InterruptedException
-     */
-    public Throwable joinAndReturn() throws InterruptedException {
-        join();
-        if (uncaught != null) {
-            return uncaught;
-        }
-        return null;
-    }
-
-    /**
-     * Waits during {@code timeout} for {@link TestThread} to die
-     * and returns exception caught during the execution.
-     *
-     * @param timeout The time to wait in milliseconds
-     * @return Exception caught during the execution
-     * @throws InterruptedException
-     */
-    public Throwable joinAndReturn(long timeout) throws InterruptedException {
-        join(timeout);
-        if (isAlive()) {
-            return new TimeoutException();
-        }
-        if (uncaught != null) {
-            return uncaught;
-        }
-        return null;
-    }
-}
--- a/test/jdk/lib/testlibrary/jdk/testlibrary/XRun.java	Mon Aug 27 17:01:15 2018 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.testlibrary;
-
-/**
- * This type serves no other purpose than to simply allow automatically running
- * something in a thread, and have all exceptions propagated to
- * RuntimeExceptions, which are thrown up to thread, which in turn should
- * probably be a {@link TestThread} to they are stored.
- */
-public abstract class XRun implements Runnable {
-
-    /**
-     * Invokes {@code xrun()} and throws all exceptions caught in it
-     * up to the thread.
-     */
-    public final void run() {
-        try {
-            xrun();
-        } catch (Error e) {
-            throw e;
-        } catch (RuntimeException e) {
-            throw e;
-        } catch (Throwable e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    /**
-     * Override this method to implement what to run in the thread.
-     *
-     * @throws Throwable
-     */
-    protected abstract void xrun() throws Throwable;
-}
--- a/test/jdk/sun/management/jmxremote/bootstrap/JMXInterfaceBindingTest.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/management/jmxremote/bootstrap/JMXInterfaceBindingTest.java	Mon Aug 27 21:50:52 2018 -0700
@@ -29,7 +29,7 @@
 import java.util.List;
 import java.util.stream.Collectors;
 
-import jdk.testlibrary.ProcessThread;
+import jdk.test.lib.thread.ProcessThread;
 import jdk.testlibrary.ProcessTools;
 
 /**
@@ -49,6 +49,7 @@
  *          interfaces to bind to (using plain sockets and SSL sockets).
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules java.management.rmi
  *
  * @build jdk.testlibrary.* JMXAgentInterfaceBinding
--- a/test/jdk/sun/tools/jstatd/JstatdTest.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/JstatdTest.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,10 +28,10 @@
 import java.rmi.registry.Registry;
 import java.util.Arrays;
 
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.thread.ProcessThread;
 import static jdk.testlibrary.Asserts.*;
 import jdk.testlibrary.JDKToolLauncher;
-import jdk.testlibrary.OutputAnalyzer;
-import jdk.testlibrary.ProcessThread;
 import jdk.testlibrary.Utils;
 import jdk.testlibrary.ProcessTools;
 
--- a/test/jdk/sun/tools/jstatd/TestJstatdDefaults.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/TestJstatdDefaults.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
  * @key intermittent
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  *
  * @build jdk.testlibrary.* JstatdTest JstatGCUtilParser
  * @run main/timeout=60 TestJstatdDefaults
--- a/test/jdk/sun/tools/jstatd/TestJstatdExternalRegistry.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/TestJstatdExternalRegistry.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
  * @key intermittent
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  *
  * @build jdk.testlibrary.* JstatdTest JstatGCUtilParser
  * @run main/timeout=60 TestJstatdExternalRegistry
--- a/test/jdk/sun/tools/jstatd/TestJstatdPort.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/TestJstatdPort.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
  * @key intermittent
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  *
  * @build jdk.testlibrary.* JstatdTest JstatGCUtilParser
  * @run main/timeout=60 TestJstatdPort
--- a/test/jdk/sun/tools/jstatd/TestJstatdPortAndServer.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/TestJstatdPortAndServer.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
  * @key intermittent
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  *
  * @build jdk.testlibrary.* JstatdTest JstatGCUtilParser
  * @run main/timeout=60 TestJstatdPortAndServer
--- a/test/jdk/sun/tools/jstatd/TestJstatdServer.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/TestJstatdServer.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
  * @key intermittent
  *
  * @library /lib/testlibrary
+ * @library /test/lib
  *
  * @build jdk.testlibrary.* JstatdTest JstatGCUtilParser
  * @run main/timeout=60 TestJstatdServer
--- a/test/jdk/sun/tools/jstatd/TestJstatdUsage.java	Mon Aug 27 17:01:15 2018 -0700
+++ b/test/jdk/sun/tools/jstatd/TestJstatdUsage.java	Mon Aug 27 21:50:52 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
  * @test
  * @bug 4990825
  * @library /lib/testlibrary
+ * @library /test/lib
  * @modules java.management
  * @build jdk.testlibrary.*
  * @run main TestJstatdUsage
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/lib/jdk/test/lib/thread/ProcessThread.java	Mon Aug 27 21:50:52 2018 -0700
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.test.lib.thread;
+
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.process.ProcessTools;
+
+import java.io.PrintWriter;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Predicate;
+
+/**
+ * The helper class for starting and stopping {@link Process} in a separate thread.
+ */
+public class ProcessThread extends TestThread {
+
+    /**
+     * Creates a new {@code ProcessThread} object.
+     *
+     * @param threadName The name of thread
+     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
+     */
+    public ProcessThread(String threadName, String... cmd) {
+        super(new ProcessRunnable(new ProcessBuilder(cmd)), threadName);
+    }
+
+    /**
+     * Creates a new {@code ProcessThread} object.
+     *
+     * @param threadName The name of thread.
+     * @param pb The ProcessBuilder to execute.
+     */
+    public ProcessThread(String threadName, ProcessBuilder pb) {
+        super(new ProcessRunnable(pb), threadName);
+    }
+
+
+    /**
+     * Creates a new {@code ProcessThread} object.
+     *
+     * @param threadName The name of thread
+     * @param waitfor A predicate to determine whether the target process has been initialized
+     * @param cmd The string array of program and its arguments to pass to {@link ProcessBuilder}
+     */
+    public ProcessThread(String threadName, Predicate<String> waitfor, String... cmd) {
+        super(new ProcessRunnable(new ProcessBuilder(cmd), threadName, waitfor), threadName);
+    }
+
+    /**
+     * Creates a new {@code ProcessThread} object.
+     *
+     * @param threadName The name of thread.
+     * @param waitfor A predicate to determine whether the target process has been initialized
+     * @param pb The ProcessBuilder to execute.
+     */
+    public ProcessThread(String threadName, Predicate<String> waitfor, ProcessBuilder pb) {
+        super(new ProcessRunnable(pb, threadName, waitfor), threadName);
+    }
+
+    /**
+     * Stops {@link Process} started by {@code ProcessRunnable}.
+     *
+     * @throws InterruptedException
+     */
+    public void stopProcess() throws InterruptedException {
+        ((ProcessRunnable) getRunnable()).stopProcess();
+    }
+
+    /**
+     * @return The process output, or null if the process has not yet completed.
+     */
+    public OutputAnalyzer getOutput() {
+        return ((ProcessRunnable) getRunnable()).getOutput();
+    }
+
+    /**
+    * Returns the PID associated with this process thread
+    * @return The PID associated with this process thread
+    */
+    public long getPid() throws InterruptedException {
+        return ((ProcessRunnable)getRunnable()).getPid();
+    }
+
+    public void sendMessage(String message) throws InterruptedException {
+        ((ProcessRunnable)getRunnable()).sendMessage(message);
+    }
+
+    /**
+     * {@link Runnable} interface for starting and stopping {@link Process}.
+     */
+    static class ProcessRunnable extends XRun {
+
+        private final ProcessBuilder processBuilder;
+        private final CountDownLatch latch;
+        private volatile Process process;
+        private volatile OutputAnalyzer output;
+        private final Predicate<String> waitfor;
+        private final String name;
+
+        /**
+         * Creates a new {@code ProcessRunnable} object.
+         *
+         * @param pb The {@link ProcessBuilder} to run.
+         */
+        public ProcessRunnable(ProcessBuilder pb) {
+            this(pb, "", null);
+        }
+
+        /**
+         * Creates a new {@code ProcessRunnable} object.
+         *
+         * @param pb The {@link ProcessBuilder} to run.
+         * @param name An optional process name; may be null
+         * @param waitfor A predicate to determine whether the target process has been initialized; may be null
+         */
+        public ProcessRunnable(ProcessBuilder pb, String name, Predicate<String> waitfor) {
+            this.processBuilder = pb;
+            this.latch = new CountDownLatch(1);
+            this.name = name;
+            this.waitfor = waitfor;
+        }
+
+        /**
+         * Starts the process in {@code ProcessThread}.
+         * All exceptions which occurs here will be caught and stored in {@code ProcessThread}.
+         *
+         * see {@link XRun}
+         */
+        @Override
+        public void xrun() throws Throwable {
+            this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
+            // Release when process is started
+            latch.countDown();
+
+            // Will block...
+            try {
+                this.process.waitFor();
+                output = new OutputAnalyzer(this.process);
+            } catch (Throwable t) {
+                String name = Thread.currentThread().getName();
+                System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString()));
+                throw t;
+            } finally {
+                this.process.destroyForcibly().waitFor();
+                String logMsg = ProcessTools.getProcessLog(processBuilder, output);
+                System.out.println(logMsg);
+            }
+        }
+
+        /**
+         * Stops the process.
+         *
+         * @throws InterruptedException
+         */
+        public void stopProcess() throws InterruptedException {
+            // Wait until process is started
+            latch.await();
+            if (this.process != null) {
+                System.out.println("ProcessThread.stopProcess() will kill process");
+                this.process.destroy();
+            }
+        }
+
+        /**
+         * Returns the OutputAnalyzer with stdout/stderr from the process.
+         * @return The process output, or null if process not completed.
+         * @throws InterruptedException
+         */
+        public OutputAnalyzer getOutput() {
+            return output;
+        }
+
+        /**
+         * Returns the PID associated with this process runnable
+         * @return The PID associated with this process runnable
+         */
+        public long getPid() throws InterruptedException {
+            return getProcess().pid();
+        }
+
+        public void sendMessage(String message) throws InterruptedException {
+            try (PrintWriter pw = new PrintWriter(this.getProcess().getOutputStream())) {
+                pw.println(message);
+                pw.flush();
+            }
+        }
+
+        private Process getProcess() throws InterruptedException {
+            latch.await();
+            return process;
+        }
+    }
+
+}