继承 Thread
public class Mythread extends Thread {
private int i=0;
public void run() {
System.out.println(++i);
}
}
实现Runnable接口
public class R implements Runnable {
private int i=0;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("run"+(++i));
}
}
测试
public class TestThread {
public static void main(String[] args) {
for(int i=0;i<10;i++){
new Mythread().start();
}
try {
new Thread().join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
R r=new R();
for(int i=0;i<10;i++){
new Thread(r).start();
}
}
}
输出结果
’1
1
run1
1
1
1
run2
1
1
run3
run4
run5
1
run7
run9
run10
1
1
run8