线程的2种表达形式:
1.继承类 extends Thread
public class ThreadTest1 {
public static void main(String[] args){
Thread1 t1 = new Thread1();
t1.start(); //Thread1中的run方法
}
}
class Thread1 extends Thread{
public void run(){ //覆盖重写extends Thread中的run
for(int i=0;i<100;i++){
System.out.println("Thread 111111-------"+i);
}
}
}
2.接口 implements Runnable
public class ThreadTest2 {
public static void main(String[] args){
MyThread mt = new MyThread();
Thread t2 = new Thread(mt);
//上面2行可以写成 Thread t2 = new Thread(new MyThread());
t2.start();
}
}
class MyThread implements Runnable{
@Override
public void run(){
for(int i=0;i<100;i++){
System.out.println("Thread 22222-------"+i);
}
}
}
两种表达形式:实现Runnable接口的代码更健壮
extends只能为单 局限性强 Runnable为接口 可以实现多个