初学者关于线程的小问题
偶遇到下面的一段代码,讲解有关线程的问题,调试后结果正常,就是想问问为什么执行后Thread1,Thread2,Thread3第一次是同时出现。按本人的理解,生成SimpleThreadDemo类时没有调用println方法,所以从程序执行开始就应该按照delay的时间显示Thread1,Thread2,Thread3。代码如下:
public class SimpleThreadDemo extends Thread
{
private String threadName;
private long delay;
public static void main(String[] args)
{
SimpleThreadDemo thread1=new SimpleThreadDemo("Thread1",1000);
SimpleThreadDemo thread2=new SimpleThreadDemo("Thread2",4000);
SimpleThreadDemo thread3=new SimpleThreadDemo("Thread3",8000);
System.out.println("Press\"Enter\"to stop...");
thread1.start();
thread2.start();
thread3.start();
try
{
System.in.read();
}
catch(Exception e)
{
}
System.out.println("end of main()");
}
public SimpleThreadDemo(String threadName,long delay)
{
this.threadName=threadName;
this.delay=delay;
setDaemon(true);
}
public void run()
{
try
{
while(true)
{
System.out.println(threadName);
sleep(delay);
}
}
catch(InterruptedException e)
{
System.out.println(threadName+"got interrupted");
}
}
}
困惑中,请指点!