请高手给我指点以下多线程的问题!这是我编写的一小程序,发现第一次运行的结果和第二次运行的结果不一样,为什么啊!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
线程结束!
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
线程结束!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货