博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程休眠
阅读量:3943 次
发布时间:2019-05-24

本文共 964 字,大约阅读时间需要 3 分钟。

每个线程都有一把锁,sleep不会释放锁

在这里插入图片描述

利用sleep来模拟倒计时

package duoxiancheng;//模拟网络延时public class TestSleep {
public static void main(String[] args) throws InterruptedException {
turnDown(); } public static void turnDown() throws InterruptedException {
int num=10; while(true){
Thread.sleep(1000); System.out.println(num--); if(num<=0){
break; } } }}

在这里插入图片描述

利用sleep打印输出当前系统时间:

package duoxiancheng;import java.text.SimpleDateFormat;import java.util.Date;//模拟网络延时public class TestSleep {
public static void main(String[] args) throws InterruptedException {
Date startTime=new Date(System.currentTimeMillis());//获取系统当前时间 while(true){
Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime)); Date smartTime=new Date(System.currentTimeMillis());//更新系统当前时间 } }}

代码与截图如下:

在这里插入图片描述

转载地址:http://wznwi.baihongyu.com/

你可能感兴趣的文章
Lesson 4 Part 2 Softmax Regression
查看>>
文章中运用到的数学公式
查看>>
Projective Dynamics: Fusing Constraint Projections for Fast Simulation
查看>>
从2D恢复出3D的数据
查看>>
glm 中 数据类型 与 原始数据(c++ 数组)之间的转换
查看>>
Derivatives of scalars, vector functions and matrices
查看>>
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
Optimizate objective function in matrix
查看>>
Convert polygon faces to triangles or quadrangles
查看>>
read obj in matlab
查看>>
find out the neighbour matrix of a mesh
查看>>
Operators and special characters in matlab
查看>>
As-Conformal-As-Possible Surface Registration
查看>>
qmake Variable Reference
查看>>
Lesson 2 Gradient Desent
查看>>
find border vertex
查看>>