作者dharma (达)
看板java
标题[问题] 不用extends Thread即可new出Thread物件
时间Sat Jul 25 21:35:18 2015
下面的程式码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://webptt.com/cn.aspx?n=bbs/java/M.1437831320.A.4E7.html
1F:→ ssccg: 因为Thread就是有Thread(Runnable)这个ctor,本来就可以 07/25 21:49
2F:→ ssccg: Thread的run原本的实作是执行ctor传进来的Runnable的run 07/25 21:52