查看源码的区别:
实现Runnable接口
弊端是:不能直接使用Thread中的方法需要先获取到线程对象后,才能得到Thread的方法,代码复杂
继承Thread类
new Thread() { //1,new 类(){}继承这个类
public void run() { //2,重写run方法
for(int i = 0; i < 3000; i++) { //3,将要执行的代码,写在run方法中
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
}
}.start();
实现Runnable接口
new Thread(new Runnable(){ //1,new 接口(){}实现这个接口
public void run() { //2,重写run方法
for(int i = 0; i < 3000; i++) { //3,将要执行的代码,写在run方法中
System.out.println("bb");
}
}
}).start();
public static void main(String[] args) {
new Thread(){
public void run(){
for (int i = 0; i < 1000; i++) {
System.out.println("继承。。");
}
}
}.start();
new Thread(new Runnable(){
@Override
public void run() {
for (int i = 0; i < 2000; i++) {
System.out.println("nuw runna");
}
}
}).start();
}
原文:http://blog.51cto.com/357712148/2156453