请高手给我指点以下多线程的问题!这是我编写的一小程序,发现第一次运行的结果和第二次运行的结果不一样,为什么啊!package com.Thread;import java.text.DateFormat;
import java.util.Date;class Thread1 { public static void main(String[] args){
thread th1=new thread("thread1");
thread th2=new thread("thread2");
     
th1.start();

th2.start();
}
}
class thread extends Thread{


thread(String str){
super(str);
}

public  void run(){
for(int i=0;i<3;i++){
System.out.println(getName()+"在运行"+new Date());

try {
sleep(5000);
System.out.println(getName()+"在休眠"+new Date());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  }
System.out.println("线程结束!");
}


}这是第一次的运行的结果!
thread1在运行Tue Oct 13 09:32:05 CST 2009
thread2在运行Tue Oct 13 09:32:05 CST 2009
thread1在休眠Tue Oct 13 09:32:10 CST 2009
thread2在休眠Tue Oct 13 09:32:10 CST 2009
thread1在运行Tue Oct 13 09:32:10 CST 2009
thread2在运行Tue Oct 13 09:32:10 CST 2009
thread2在休眠Tue Oct 13 09:32:15 CST 2009
thread2在运行Tue Oct 13 09:32:15 CST 2009
thread1在休眠Tue Oct 13 09:32:15 CST 2009
thread1在运行Tue Oct 13 09:32:15 CST 2009
thread2在休眠Tue Oct 13 09:32:20 CST 2009
线程结束!
thread1在休眠Tue Oct 13 09:32:20 CST 2009
线程结束!第二次的运行的结果:thread2在运行Tue Oct 13 09:33:19 CST 2009
thread1在运行Tue Oct 13 09:33:19 CST 2009
thread1在休眠Tue Oct 13 09:33:24 CST 2009
thread1在运行Tue Oct 13 09:33:24 CST 2009
thread2在休眠Tue Oct 13 09:33:24 CST 2009
thread2在运行Tue Oct 13 09:33:24 CST 2009
thread1在休眠Tue Oct 13 09:33:29 CST 2009
thread1在运行Tue Oct 13 09:33:29 CST 2009
thread2在休眠Tue Oct 13 09:33:29 CST 2009
thread2在运行Tue Oct 13 09:33:29 CST 2009
thread1在休眠Tue Oct 13 09:33:34 CST 2009
线程结束!
thread2在休眠Tue Oct 13 09:33:34 CST 2009
线程结束!

解决方案 »

  1.   

    很正常,线程何时启动运行本来就有不确定性
      

  2.   

    两次运行结果不一样很正常啊就象两个人跑步,不见得两次比赛的结果总是一样吧
      

  3.   

    多线程问题就像 多人长跑!每次比赛 比赛的过程和结果 肯定是不一样的!如果都一样那就是单线程了!
      

  4.   

    不一样是正常的多线程是这样的,不然就不叫多线程了