[英]Can a class have two run methods for two different threads in Java?
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)。 我怎樣才能做到這一點???
該類應為sleepTime
使用其他構造sleepTime
。
public class MyRunnable implements Runnable() {
private final long sleepTime;
public MyRunnable(long sleepTime) {
this.sleepTime = sleepTime;
}
@Override public void run() {
// ...
Thread.sleep(sleepTime);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.