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
新闻热点
疑难解答