[问题] 不用extends Thread即可new出Thread物件

楼主: 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(); // 启动执行绪
...
}
}
作者: ssccg (23)   2015-07-25 21:49:00
因为Thread就是有Thread(Runnable)这个ctor,本来就可以Thread的run原本的实作是执行ctor传进来的Runnable的run
作者: MonyemLi (life)   2015-07-26 11:12:00
http://goo.gl/N1RtP Thread的doc

Links booklink

Contact Us: admin [ a t ] ucptt.com