反射与子进程

2017-06-21  本文已影响0人  博瑜

反射是在本身进程中的,而子进程开设新进程:

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后

image.png

而子进程开设新进程:

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后

image.png

这个类似于worker中启动executor

上一篇 下一篇

猜你喜欢

热点阅读