在Thread类中有个sleep(long millis)的静态方法,此方法用于线程的休眠。
package ThreadTest;
public class TwoThreadSleep extends Thread{
public void run(){
loop();
}
public void loop(){
String name =Thread.currentThread().getName();
System.out.println(name+"------>>刚进入LOOP方法");
for(int i=0;i<10;i++){
try{
Thread.sleep(2000);
}catch(InterruptedException e){}
System.out.println("name="+name);
}
System.out.println(name+"------>>离开LOOP方法");
}
public static void main(String[] args){
TwoThreadSleep tt = new TwoThreadSleep();
tt.setName("my worker thread");
tt.start();
try{
Thread.sleep(700);
}catch(InterruptedException x){}
tt.loop();
}
}