作者:
dharma (é”)
2015-07-25 21:35:18下面的程式碼A要先extends Thread
才能做:TimerThread newThread = new TimerThread();
為什麼程式碼B沒有extends Thread之類的動作
就能使用Thread:Thread newThread = new Thread(test)
為什麼呢
thanks
程式碼A:
class TimerThread extends Thread { //執行緒
public void run() { // 執行緒要執行的內容
...
}
}
public class TestThread {
public static void main(String[] argv) {
TimerThread newThread = new TimerThread();
newThread.start(); // 啟動執行緒
...
}
}
程式碼B:
class TimerThread implements Runnable {//以Runnable介面建立執行緒
public void run() { // 執行緒要執行的內容
...
}
}
public class TestRunnable {
public static void main(String[] argv) {
TimerThread test = new TimerThread();
Thread newThread = new Thread(test)
newThread.start(); // 啟動執行緒
...
}
}