一般我们使用Java运行其他类中的方法的时候,无论是静态调用还是动态调用,都是在当前的进程中执行的。也就是只有一个Java虚拟机实例在运行。有时候需要通过Java代码启动多个Java子进程,这样做会消耗些资源,但是程序变得更稳定。因为新启动的进程是在不同的虚拟机中运行的。

在Windows中,一个虚拟机就是一个

多线程 调用多线程的方法 Runtime与ProcessBuilder-LMLPHP


有两种方式调用一个进程

1、System.exec

子进程:

 package org.zln.thread;

 import java.io.File;
import java.io.IOException; /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestFile {
public static void main(String[] args) {
try {
File file = new File("D:\\my.txt");
file.createNewFile();
System.out.println("被调用成功!"); } catch (IOException e) {
e.printStackTrace();
}
}
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestFile.java

主进程:

 package org.zln.thread;

 import java.io.IOException;

 /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestRuntime {
public static void main(String[] args) {
String rootPath = "E:\\GitHub\\tools\\JavaEEDevelop\\out\\production\\Lesson1_JavaSe_Demo1";
String mainPath = "org.zln.thread.TestFile";
String command = "java -classpath "+rootPath+" "+mainPath;
Runtime runtime = Runtime.getRuntime();
try {
System.out.println(command);
runtime.exec(command); } catch (IOException e) {
e.printStackTrace();
}
}
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestRuntime.java

调用发现在指定目录下创建了文件,但是并没有在控制台输出信息。因为TestFile子进程并没有自己的控制台,改进代码

 package org.zln.thread;

 import java.io.*;

 /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestRuntime {
public static void main(String[] args) {
String rootPath = "E:\\GitHub\\tools\\JavaEEDevelop\\out\\production\\Lesson1_JavaSe_Demo1";
String mainPath = "org.zln.thread.TestFile";
String command = "java -classpath "+rootPath+" "+mainPath;
Runtime runtime = Runtime.getRuntime();
try {
System.out.println(command);
Process process = runtime.exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getInputStream())));
String line;
while ((line = bufferedReader.readLine())!=null){
System.out.println("子进程输出:"+line);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestRuntime.java

这里不知为何,在IDE中运行,输出的是乱码,在控制台运行则不是乱码

既然父进程可以获取到子进程的输出,那么父进程如何发送消息给子进程呢?

子进程修改:

 package org.zln.thread;

 import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader; /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestFile {
public static void main(String[] args) {
try {
File file = new File("D:\\my.txt");
file.createNewFile();
System.out.println("被调用成功!"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("父进程输入信息:"+bufferedReader.readLine()); } catch (IOException e) {
e.printStackTrace();
}
}
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestFile.java

父进程修改:

 package org.zln.thread;

 import java.io.*;

 /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestRuntime {
public static void main(String[] args) {
String rootPath = "E:\\GitHub\\tools\\JavaEEDevelop\\out\\production\\Lesson1_JavaSe_Demo1";
String mainPath = "org.zln.thread.TestFile";
String command = "java -classpath "+rootPath+" "+mainPath;
Runtime runtime = Runtime.getRuntime();
try {
System.out.println(command);
Process process = runtime.exec(command);
/*向子进程输入*/
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bufferedWriter.write("Hello 子进程!");
bufferedWriter.close();/*必须现在就关闭,否则无法向子进程输入信息*/
/*获取子进程输出*/
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getInputStream())));
String line;
while ((line = bufferedReader.readLine())!=null){
System.out.println("子进程输出:"+line);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestRuntime.java


2、使用ProcessBuilder建立子进程

 package org.zln.thread;

 import java.io.*;

 /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestProcessBuilder {
public static void main(String[] args) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder("java","org.zln.thread.TestFile");
/*设置工作目录*/
processBuilder.directory(new File("E:\\GitHub\\tools\\JavaEEDevelop\\out\\production\\Lesson1_JavaSe_Demo1"));
Process process = processBuilder.start(); /*向子进程输入*/
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bufferedWriter.write("Hello 子进程!");
bufferedWriter.close();/*必须现在就关闭,否则无法向子进程输入信息*/
/*获取子进程输出*/
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getInputStream())));
String line;
while ((line = bufferedReader.readLine())!=null){
System.out.println("子进程输出:"+line);
}
bufferedReader.close(); }
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestProcessBuilder.java

04-14 18:48