代码示例
public
static
void
main(String args[]) {
Thread t =
new
Thread() {
public
void
run() {
pong();
}
};
t.start();
System.out.print(
"ping"
);
}
static
void
pong() {
System.out.print(
"pong"
);
}
输出结果: pingpong
12345678910111213public
static
void
main(String args[]) {
Thread t =
new
Thread() {
public
void
run() {
pong();
}
};
t.run();
System.out.print(
"ping"
);
}
static
void
pong() {
System.out.print(
"pong"
);
}
输出结果:pongping
通过以上两个程序实例,可以很容易的区分出start()方法和run()方法的区别:
t.start(); 该行代码相当于是启动线程,
t.run(); 该行代码相当于是使用t这个类中的run方法而已.