多线程的休眠

来源:互联网 发布:程序员老公的好处 编辑:程序博客网 时间:2024/06/05 20:15

在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();}

}

原创粉丝点击