1、实现Runnable接口
调用:
class 类名 implement Runnable{
public void run(){
}
}
调用:
类名 a=new 类名();
Thread t=new Thread(a);
t.start();
2、两种方法的比较
1.extends Thread 单继承2.implements Runnable 多实现
例子:
public class TheRunnable implements Runnable{
public void run() {
for(int i=0;i<10;i++){
try {
Thread.sleep(100);//睡眠100毫秒
} catch (Exception e) {
e.getMessage();
}
System.out.println("the Runnable!!!"+i);
}
}
}
测试:
public class Text {
public static void main(String[] args) {
TheRunnable tr=new TheRunnable();
Thread t=new Thread(tr);
t.start();
}
}