💛线程休眠的意思, Thread.sleep(毫秒数);

💛sleep()会产生InterruptedException异常;

💛休眠时间达到后线程进入就绪状态.

💛sleep()可以用来模拟网络延迟,倒计时等.

💛每一个对象都有一把锁, sleep()不会释放锁.

package com.smile.test.thread;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestSleep {
    public static void main(String[] args) throws InterruptedException {
        Daojishi();
        getSystemTime();
    }
    private static void Daojishi() throws InterruptedException {
        int i = 10;
        while (i>=0){
            Thread.sleep(1000);
            System.out.println(i--);
        }
    }
    private static void getSystemTime () throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Date date = new Date();
            String str = new SimpleDateFormat("HH:mm:ss").format(date);
            System.out.println(str);
            Thread.sleep(1000);
        }
    }
}

模拟倒计时:
10
9
8
7
6
5
4
3
2
1
0
Process finished with exit code 0
    
每隔一秒获取系统时间:
20:07:26
20:07:27
20:07:28
20:07:29
20:07:30
20:07:31
20:07:32
20:07:33
20:07:34
20:07:35
Process finished with exit code 0