### Question:
PidRuntimeFactory { public @Nonnull ThreadDumpRuntime fromProcess(long pid) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(jstackBinary(), "-l", Long.toString(pid)); Process process = pb.start(); ThreadDumpRuntime runtime = null; RuntimeException runtimeEx = null; try { runtime = createRuntime(process); } catch (IllegalRuntimeStateException ex) { runtimeEx = ex; } int ret = process.waitFor(); validateResult(process, ret); if (runtimeEx != null) throw runtimeEx; return runtime; } PidRuntimeFactory();  PidRuntimeFactory(@Nonnull String javaHome); PidRuntimeFactory failOnErrors(boolean failOnErrors); @Nonnull ThreadDumpRuntime fromProcess(long pid); @Nonnull ThreadDumpRuntime fromProcess(int pid); @Nonnull ThreadDumpRuntime fromProcess(@Nonnull Process process); @Nonnull ThreadDumpRuntime fromCurrentProcess();  }### Answer:
@Test public void invokeFactory() throws Exception { disposer.register(TestThread.setupSleepingThreadWithLock()); ThreadDumpRuntime pidRuntime = FACTORY.fromProcess(Util.currentPid()); ThreadDumpThread thread = pidRuntime.getThreads().where(nameIs("sleepingThreadWithLock")).onlyThread(); assertThat(thread.getStatus(), equalTo(ThreadStatus.SLEEPING)); assertThat(thread.getAcquiredLocks(), not(Matchers.<ThreadLock>emptyIterable())); assertThat( only(thread.getAcquiredLocks()).getClassName(), equalTo("java.util.concurrent.locks.ReentrantLock$NonfairSync") ); }
@Test public void notAJavaProcess() throws Exception { try { new PidRuntimeFactory().fromProcess(299); fail("No exception thrown"); } catch(IOException ex) { assertThat(ex.getMessage(), containsString("jstack failed with code ")); } }
@Test public void fromJavaLangProcess() throws Exception { Process process = disposer.register(TestThread.runJmxObservableProcess(false)).getNativeProcess(); new PidRuntimeFactory().fromProcess(process); process.destroy(); process.waitFor(); try { new PidRuntimeFactory().fromProcess(process); fail(); } catch (IllegalStateException ex) { assertThat(ex.getMessage(), startsWith("Process terminated")); } }