[問題] 不用extends Thread即可new出Thread物件
下面的程式碼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(); // 啟動執行緒
...
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.65.89.53
※ 文章網址: https://www.ptt.cc/bbs/java/M.1437831320.A.4E7.html
→
07/25 21:49, , 1F
07/25 21:49, 1F
→
07/25 21:52, , 2F
07/25 21:52, 2F
→
07/26 11:12, , 3F
07/26 11:12, 3F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章