📄 processclassstop.java
字号:
/** * Test that class processing is unaffected by thread stopping in * static initializers. * * @author Godmar Back <gback@cs.utah.edu> */import java.util.Vector;import java.lang.reflect.*;class Base { static boolean useme; private static void killme() { throw new ThreadDeath(); } static { Base b = new Base(); ProcessClassStop.v.addElement(b); useme = true; killme(); } public String toString() { return "a base"; }}public class ProcessClassStop{ static Vector v = new Vector(); public static void main(String av[]) throws Exception { // a watchdog thread that kills us off after 3 sec Thread wd = new Thread() { public void run() { try { Thread.sleep(3000); System.out.println("sorry, you timed out"); System.exit(-1); } catch (Exception e) { System.out.println(e); } } }; wd.setDaemon(true); wd.start(); // a thread that will load Base Thread t = new Thread() { public void run() { try { Class.forName("Base"); } catch (Throwable tx) { // System.out.println(tx); } } }; t.start(); Thread.sleep(1000); // nobody knows yet whether that should succeed... Thread t0 = new Thread() { public void run() { try { System.out.println("tan " + new Base()); } catch (Throwable tt) { System.out.println("tan " + tt); } } }; t0.start(); t0.join(); Class cc = v.elementAt(0).getClass(); final Object ni = cc.newInstance();/*** Thread other = new Thread() { public void run() { System.out.println(ni); } }; other.start(); other.join();***/ System.out.println("m2 " + ni); Thread t2 = new Thread() { public void run() { try { System.out.println("t2 " + v.elementAt(0).getClass().newInstance()); } catch (Throwable ty) { System.out.println(ty); } } }; t2.start(); t2.join(); System.exit(0); }}// javac flags: -nowarn/* Expected Output:tan java.lang.NoClassDefFoundError: Basejava.lang.NoClassDefFoundError: Base at java.lang.Class.getConstructor0 (Class.java) at java.lang.Class.getDeclaredConstructor (Class.java:174) at java.lang.Class.newInstance (Class.java:422) at ProcessClassStop.main (ProcessClassStop.java:74)*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -