java线程学习之Sleep方法
sleep方法是在线程中常用到的一个方法,它是一个静态方法。
sleep(long millis) 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。它可能会抛出中断异常
InterruptedException。它在Thread中定义的为:
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds, subject to
* the precision and accuracy of system timers and schedulers. The thread
* does not lose ownership of any monitors.
*
* @param millis
* the length of time to sleep in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public static native void sleep(long millis) throws InterruptedException;
sleep(long millis, int nanos)
在指定的毫秒数加指定的纳秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
在jdk的Thread中定义如下
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds plus the specified
* number of nanoseconds, subject to the precision and accuracy of system
* timers and schedulers. The thread does not lose ownership of any
* monitors.
*
* @param millis
* the length of time to sleep in milliseconds
*
* @param nanos
* {@code 0-999999} additional nanoseconds to sleep
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative, or the value of
* {@code nanos} is not in the range {@code 0-999999}
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public static void sleep(long millis, int nanos)
throws InterruptedException {
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
} if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
millis++;
} sleep(millis);
}
例子一:
package com.song.test;
import java.util.Date;
public class TestThread01 extends Thread {
public static void main(String[] args) {
TestThread01 test = new TestThread01();
test.start();
}
@Override
public void run() {
long time1 = System.currentTimeMillis();
System.out.println("现在时间" + time1);
try {
sleep(2000);
long time2= System.currentTimeMillis();
System.out.println("休眠2秒后" + time2);
System.out.println(time2-time1);
sleep(1000,500000);
long time3 = System.currentTimeMillis();
System.out.println("休眠1000.5后:" + time3);
System.out.println(time3-time2);//因为时间精度为毫秒,会造成误差
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
}
运行结果为:

java线程学习之Sleep方法的更多相关文章
- java线程学习之join方法
join()方法表示一个线程要加入另一个线程,直到被加入的线程执行完毕. 这个概念不好理解的话看面这个例子 public class TestJoin { public static void mai ...
- java线程学习之wait方法
wait 等待方法是让线程进入等待队列,使用方法是 obj.wait(); 这样当前线程就会暂停运行,并且进入obj的等待队列中,称作“线程正在obj上等待”. 如果线程想执行 wait 方法,线程必 ...
- java线程学习之yield方法
yield方法是暂停当前正在执行的线程对象,并执行其他线程. 这是一个静态方法,一旦执行,它会使当前线程让出CPU.让出的cpu并不代表当前线程不执行了.当前线程让出CPU后,还会CPU资源的争夺,但 ...
- Java线程学习详解
线程基础 1. 线程的生命周期 1.1 新建状态: 使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态.它保持这个状态直到程序 start() 这个线程. 1 ...
- 模拟做饭系统(java+线程中的join方法)
(一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...
- 停止Java线程,小心interrupt()方法
来源:http://blog.csdn.net/wxwzy738/article/details/8516253 程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决 ...
- java 线程学习
转载:详见处http://lavasoft.blog.51cto.com/62575/27069 Java多线程编程总结 下面是Java线程系列博文的一个编目: Java线程:概念与原理 ...
- 学习java线程学习笔记
线程:代码执行的一个分支 主要作用是提高了效率,cpu能同时执行多个部分的代码. 线程的创建:两种方式 a.继承于thread类,重写run方法. b. ...
- JAVA线程sleep和wait方法区别
一. sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复,调用sleep 不会释放对象锁.由于没有释放对象锁,所以不能 ...
随机推荐
- 六、web应用与Tomcat
软件系统体系结构 1 常见软件系统体系结构B/S.C/S 1.1 C/S l C/S结构即客户端/服务器(Client/Server),例如QQ: l 需要编写服务器端程序,以及客户端程序,例如我们安 ...
- 如何设置记事本( .txt文件)的默认编码为UTF-8?
1.在桌面新建一个文本文档,不要写入任何内容,然后手动另存为,将此文档编码改为UTF-8,然后将文件名字改为template.txt: 2.再将template.txt移动到C:\Windows\Sh ...
- Android字符串,颜色,尺寸资源的使用
字符串.颜色.尺寸资源文件这三种文件位于res文件夹的values文件夹中,名称分别为strings.xml , colors.xml , dimens.xml下面是例子,首先来看字符串资源文件str ...
- 解决python3 pip安装、更新及yaml安装
问题:python3.6版本使用pip安装第三方库时总是报错 电脑中存在多个python版本写成对应pip版本 解决:pip3 install pyOpenSSL -i http://pypi.dou ...
- ASCII Unicode UTF-8 之间的关系
转载请标明:https://i.cnblogs.com/EditPosts.aspx?opt=1 1. ASCII ASCII 只有127个字符,表示英文字母的大小写.数字和一些符号,但由于其他语言用 ...
- 在linux系统中出现u盘问题 的相关解决方法
1.显示unknown filesystem type .exfat 可以通过该方法解决: 安装exfat-fuse: 在终端中以管理员身份运行 sudo apt-get install exfat- ...
- 记录请求的耗时(拦截器、过滤器、aspect)
文章前言 记录控制器请求的耗时处理通常有三种实现方式,分别是:过滤器.拦截器.aspect:下文将逐一实现. 1.Filter 过滤器 1.1.方法说明 需要实现 Filter 类,主要涉及三个方法: ...
- 学习记录----简单的原生js路由
在以前的web程序中,路由字眼只出现在后台中.但是随着SPA单页面程序的发展,便出现了前端路由一说.单页面顾名思义就是一个网站只有一个html页面,但是点击不同的导航显示不同的内容,对应的url也会发 ...
- Virtual Memory is deprecated in Redis 2.4
在读一个源码的讲解的文章时或者读一本关于某个技术的数据集时,可能书籍的讲解是滞后的,就是没有更上最新的代码,那么就要注意了WARNING! Virtual Memory is deprecated i ...
- 2018-2019-2 网络对抗技术 20165321 Exp4 恶意代码分析
1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,sys ...