public void run(){
try{for(int i=5;i>0;i--){
System.out.println("My Thread: " +i);
t.sleep(2000);
}//end of for
}catch(InterruptedException e){System.out.println("MyThread Interrupted");}
System.out.println("MyThread exiting"); }
}//end of class
就像我想要两个线程一样,唯一的区别是t.sleep(1000)和t1.sleep(2000)。我怎么能这样做?
答案 0 :(得分:2)
该类应该为sleepTime
添加一个额外的构造函数。
public class MyRunnable implements Runnable() {
private final long sleepTime;
public MyRunnable(long sleepTime) {
this.sleepTime = sleepTime;
}
@Override public void run() {
// ...
Thread.sleep(sleepTime);
}