假设我们有一个简单的Java程序,它只包含一个类:
public class HelloWorld {
private static void replacable(int i) {
System.out.println("Today is a nice day with a number " + i);
}
public static void main(String[] args) throws Exception {
for(int i = 0; i < 100000; ++i) {
replacable(i);
Thread.sleep(500);
}
}
Run Code Online (Sandbox Code Playgroud)
在编译并运行之后,输出将是:
今天是0号的好日子
今天是一个美好的一天,1号
今天是2号球员的好日子
今天是一个美好的一天,3号
...
我的问题:是否存在(或者是否存在)replacable在运行时交换方法的某种方式?有点像HelloWorld用新版本编写另一个版本replacable,编译它,然后在已经运行的JVM中编写旧版本?
所以,如果我像这样编写新版本:
private static void replacable(int i) {
System.out.println("Today is an even nicer day with a number " + i);
}
Run Code Online (Sandbox Code Playgroud)
是否有类似于Erlang的热代码交换,我可以这样做: …