线程第七课,sleep示例代码

来源:互联网 发布:二级c语言上机题库 编辑:程序博客网 时间:2024/06/05 21:56

sleep:休眠,不会释放锁

1、与时间相关:倒计时

package com.pkushutong.DemoThread;import java.text.SimpleDateFormat;import java.util.Date;/** * sleep:倒计时 * 1、倒数10个数,一秒内打印一个 * 2、倒计时 * */public class Test06{public static void main(String[] args) throws InterruptedException  {Date time = new Date(System.currentTimeMillis()+10*1000);//当前时间+10秒long end = time.getTime();while(true){//输出时间System.out.println(new SimpleDateFormat("mm:ss").format(time));//构建下一秒的时间time = new Date(time.getTime() - 1000);//等待一秒Thread.sleep(1000);//10秒以内继续,否则退出if(end - 10000 > time.getTime()){break;}}}public static void test() throws InterruptedException{int num = 10;while(true){System.out.println(num--);Thread.sleep(1000);//暂停if(num <= 0){break;}}}}


2、模拟网络延时 


0 0
原创粉丝点击