public class StaticSample {
public static void main(String[] args) {
Thread thread = new Thread() {
public void run() { hai(); }
};
thread.run(); // 直接呼叫 run(),非啟動新執行緒
System.out.print("you");
}
static void hai() { System.out.print("hai"); }
}
輸出結果: haiyou
public class StaticSample {
public static void main(String[] args) {
Thread thread = new Thread() {
public void run() { hai(); }
};
thread.run();
System.out.println("you");
}
static void hai() { System.out.println("hai"); }
}
輸出結果:
hai
you
關鍵點: 呼叫 thread.run() 只是在主執行緒中同步執行 run() 方法,並未啟動新執行緒;若要啟動新執行緒必須呼叫 thread.start()。static 方法 hai() 可直接由靜態上下文呼叫。println 會換行,print 不換行,故輸出格式不同。