java中只允許單一繼承,但允許實現多個接口,因此第二種方法更靈活。
public void start() {
this.running = true;
super.start();
}
public void run() {
int i = 0;
try {
while (running) {
System.out.println("繼承Thread類定義線程程序體......" + i++);
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void startThreadA() {
System.out.println("啟動繼承Thread類定義線程");
this.start();
}
public void stopThreadA() {
System.out.println("關閉繼承Thread類定義線程");
this.running = false;
}
}
public void run() {
System.out.println("實現Runnable接口定義線程程序體......");
this.runDate = new Date();
System.out.println("線程啟動時間......" + runDate);
}
啟動繼承Thread類定義線程
繼承Thread類定義線程程序體......0
繼承Thread類定義線程程序體......1
繼承Thread類定義線程程序體......2
繼承Thread類定義線程程序體......3
繼承Thread類定義線程程序體......4
關閉繼承Thread類定義線程
實現Runnable接口定義線程程序體......
線程啟動時間......Fri Mar 15 12:56:57 CST 2013
新聞熱點
疑難解答