用线程里面的sleep设置一个倒计时程序

package thread;

import java.util.Scanner;
/**
 * 
 * Thread提供了一个静态方法 static void sleep(long ms) 该方法允许运行这个方法的线程进入阻塞状态指定毫秒
 * 超时后线程会自动回到,RUNNABLE状态等待继续并发运行
 * 
 * 通常使用sleep方法控制线程周期性执行任务(如定时器)
 * 
 * @author 清风已来
 *
 */

public class Thread_sleep {
	public static void main(String[] args) {
		/*
		 * 倒计时程序
		 * 程序启动后要求用户输入一个数字
		 * 然后每秒递减1并输入该数字,递减至0时程序停止
		 */
		System.out.println("各单位注意倒计时开始了!");
		System.out.println("请首长设置倒计时时间!");
		Scanner scanner =new Scanner(System.in);
		int num =scanner.nextInt();
		System.out.println("倒计时开始!");
		/*
		for(int i=num; i>=0;i--) {
			System.out.println(i);
			try {
				Thread.sleep(1000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
		}
		*///for循环以及while循环
		while(num>=0) {		
		System.out.println(num--);
		try {
			Thread.sleep(1000);
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	  }
		System.out.println("导弹发射!!!");
	}
}

  

原文地址:https://www.cnblogs.com/xyk1987/p/8260174.html