以下代码调用 t.run();运行如下:
public class MyUtil extends ArrayList {
public static void main(String [] args) {
Thread t = new Thread(){
public void run(){
pong();
}
};
t.run();
System.out.println("ping");
}
static void pong(){
System.out.println("pong");
}
}
运行结果为:
pong
ping
以下代码调用 t.start();运行如下:
public class MyUtil extends ArrayList {
public static void main(String [] args) {
Thread t = new Thread(){
public void run(){
pong();
}
};
t.start();
System.out.println("ping");
}
static void pong(){
System.out.println("pong");
}
}
运行结果为:
ping
pong
考点:
run() 运行线程,相当于调用普通run()方法,所以先执行pong 再执行ping
start() 启动线程,将开启线程执行主线程run()方法ping,在执行pong