Przeglądaj źródła

add child-process fixtures: hello, io, executeStatus

yangyi 1 tydzień temu
rodzic
commit
ee5d383276

+ 9 - 0
src/main/resources/code/executeStatus/Main.java

@@ -0,0 +1,9 @@
+/**
+ * 子进程程序:获取状态码示例使用的固定程序。
+ * 启动后睡眠 10 秒再退出,用于演示 waitFor(timeout) 超时与 destroy() 销毁流程。
+ */
+public class Main {
+    public static void main(String[] args) throws InterruptedException {
+        Thread.sleep(10 * 1000L);
+    }
+}

+ 10 - 0
src/main/resources/code/hello/Main.java

@@ -0,0 +1,10 @@
+/**
+ * 子进程程序:ProcessBuilder 构建示例使用的固定程序。
+ * 启动后向标准输出打印两行文字,随后正常退出(退出码 0)。
+ */
+public class Main {
+    public static void main(String[] args) {
+        System.out.println("Hello from child process!");
+        System.out.println("child pid = " + ProcessHandle.current().pid());
+    }
+}

+ 21 - 0
src/main/resources/code/io/Main.java

@@ -0,0 +1,21 @@
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+/**
+ * 子进程程序:标准输入 / 标准输出 / 标准错误演示程序。
+ * 从标准输入逐行读取并回显到标准输出;
+ * 读到 EOF(无更多输入)后向标准错误打印统计信息,随后退出。
+ */
+public class Main {
+    public static void main(String[] args) throws IOException {
+        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
+        String line;
+        int count = 0;
+        while ((line = stdin.readLine()) != null) {
+            System.out.println("echo: " + line);
+            count++;
+        }
+        System.err.println("child: read " + count + " line(s) from stdin");
+    }
+}