下面代码的运行结果是什么?public test extends Thread{ public void run(){
System.out.println(Thread.currentThread()==this);
} public static void main(String[] args){
test the=new test();
the.run();
the.start();
}}
System.out.println(Thread.currentThread()==this);
} public static void main(String[] args){
test the=new test();
the.run();
the.start();
}}
而run()方法,只是Thread类的一个方法,它本身并不能开辟线程。
所以,当你用run()时候,程序只有一个线程,根本没达到你多线程的目的(是错误的)。而你用start()时候,你的程序又开辟了两个线程,所以结果不同
up
买本 SCJP 看看,这本书看完,能明白一些有关线程的问题,顺便你还能考个证回来
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。记住:线程就是为了更好地利用CPU,
提高程序运行速率的!