【发布时间】:2019-02-11 22:22:36
【问题描述】:
我正在研究实现 Java 类的一些热重载的方法。我正在考虑的技术是这样的:
- 将外部/核心库中的所有类保存在内存中
- 如果我的某个文件发生更改,请从内存中删除所有 my 类并重新加载我的所有类。我不必重新加载内存中的任何库,因为它们不会改变并且不依赖于我的文件/类。
public class Server implements Runnable {
private Thread current;
public void run(){
// create a new classloader and load all my classes from disk?
}
public Server start(){
if(this.current != null){
this.current.destroy(); // not sure if this works
}
this.current = new Thread(r);
this.current.start();
return this;
}
public static void main(String[] args){
var s = new Server().start();
onFileChanges(filePath -> {
// we don't really care what file changed
// as long as it's one of our project's files, we reload all classes
s.start();
});
}
}
我认为关键的想法是我可以从我的项目中重新加载所有类,而不是尝试计算一些依赖关系树。
我的主要问题是 -
(a) 如何停止/终止线程? Thread#destroy 已弃用。
(b) 如何从内存中的类加载器中删除所有类?
(c) 如何将库的所有类保留在内存中,但从内存中删除对我的代码的类/实例的所有引用?
有人认为这种技术会奏效吗?是否可以实现?
【问题讨论】:
-
是的,每次我重新加载所有类时都使用一个新线程可能没有必要,我不确定