class ThreadDemo extends Thread
{
private int time = 0;
public ThreadDemo(int time, Runnable target, String name){
super(target,name);
this.time = time;
}
public synchronized void start() {
if(this.time>0){
try {
sleep(time*1000);
System.out.println("time:"+this.time);
super.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
super.start();
}
}
public void run()
{
for (int x=0; x<100;x++ )
{
System.out.println(Thread.currentThread().getName()+"....."+x);
}
}
public static void main(String[] args) throws Exception
{
Demo d1=new Demo();
Demo d2=new Demo();
ThreadDemo t1=new ThreadDemo(3,d1,"1111111111");
ThreadDemo t2=new ThreadDemo(1,d2,"2222222222");
t1.start();
t2.start();
}
}
class Demo implements Runnable
{
public void run()
{
for (int x=0; x<100;x++ )
{
System.out.println(Thread.currentThread().getName()+"....."+x);
}
}
}
帅哥,给你调试了一下,应该是在运行的线程开始前进行睡眠。 |