public class TestReloadClass {
private static long startTime = System.currentTimeMillis();
public static Class reloadClass(Class cls) throws Exception {
String path = cls.getResource(cls.getSimpleName() + ".class").getPath();
File file = new File(path);
if (file.exists()) {
System.out.println("file:" + path);
System.out.println("start:" + startTime);
System.out.println("modif:" + file.lastModified());
if (file.lastModified() > startTime) {
System.out.println("reload new class:" + cls.getName());
return Class.forName(cls.getName());
}
}
return null;
} /**
* @param args
*/
public static void main(String[] args) throws Exception {
Map m = new HashMap();
m.put(TestHotThread.class.getName(), TestHotThread.class);
while (true) {
Set entrys = m.entrySet();
Iterator iter = entrys.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
Class cls = (Class)(entry.getValue());
Thread t = (Thread)cls.newInstance();
t.start();
Thread.sleep(10000);
if ((cls = reloadClass(cls)) != null)
entry.setValue(cls);
}
}
}
}public class TestHotThread extends Thread {
public void run() {
System.out.println("thread is runni..");
try {
this.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub }}类重新用Class.forName生成了class,重新放在map中,为什么下一次运行的还是老的class呢?
private static long startTime = System.currentTimeMillis();
public static Class reloadClass(Class cls) throws Exception {
String path = cls.getResource(cls.getSimpleName() + ".class").getPath();
File file = new File(path);
if (file.exists()) {
System.out.println("file:" + path);
System.out.println("start:" + startTime);
System.out.println("modif:" + file.lastModified());
if (file.lastModified() > startTime) {
System.out.println("reload new class:" + cls.getName());
return Class.forName(cls.getName());
}
}
return null;
} /**
* @param args
*/
public static void main(String[] args) throws Exception {
Map m = new HashMap();
m.put(TestHotThread.class.getName(), TestHotThread.class);
while (true) {
Set entrys = m.entrySet();
Iterator iter = entrys.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
Class cls = (Class)(entry.getValue());
Thread t = (Thread)cls.newInstance();
t.start();
Thread.sleep(10000);
if ((cls = reloadClass(cls)) != null)
entry.setValue(cls);
}
}
}
}public class TestHotThread extends Thread {
public void run() {
System.out.println("thread is runni..");
try {
this.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub }}类重新用Class.forName生成了class,重新放在map中,为什么下一次运行的还是老的class呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货