反射是在本身进程中的,而子进程开设新进程:
package javatest.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainReflect {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class clazz = Class.forName("javatest.test.Jchild");
Method mainMethod = clazz.getMethod("main", String[].class);
mainMethod.invoke(null, (Object) new String[]{"aaa"});
}
}
package javatest.test;
public class Jchild {
public static void main(String[] args) {
System.out.println("111");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
jps后
而子进程开设新进程:
package javatest.test;
import java.io.IOException;
public class JVMTest {
public static void main(String[] args) {
Process process = null;
try {
process = Runtime.getRuntime().exec("/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java -classpath /Users/cs201/javatest/bin/ javatest.test.Jchild");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
jps后
这个类似于worker中启动executor