休眠线程

休眠线程

Thread.sleep()       让线程睡觉去

Thread.sleep(1000)    让线程睡1000ms

public class demon3_sleep {
    //休眠线程
    public static void main(String[] args) throws InterruptedException {
        demo1(); 
        new Thread(){
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                    System.out.println(getName() + "..aaaaa");   //  getName() 获取线程名
                }
                
            }
            
        }.start();
        new Thread(){    
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();    //  JVM 默认调用  printStackTrace()方法, 获取异常类名 和异常信息及异常位置
                    }
                    System.out.println(getName() + "..bb");
                }
            }
            
        }.start();
    }

    public static void demo1() throws InterruptedException {
        for (int i = 20; i>=0; i--) {    //  倒计时
            Thread.sleep(1000);
            System.out.println("倒计时:" + i + "秒");
        }
    }

}
竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
原文地址:https://www.cnblogs.com/yaobiluo/p/11346743.html