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方法的更多相关文章

  1. java线程学习之join方法

    join()方法表示一个线程要加入另一个线程,直到被加入的线程执行完毕. 这个概念不好理解的话看面这个例子 public class TestJoin { public static void mai ...

  2. java线程学习之wait方法

    wait 等待方法是让线程进入等待队列,使用方法是 obj.wait(); 这样当前线程就会暂停运行,并且进入obj的等待队列中,称作“线程正在obj上等待”. 如果线程想执行 wait 方法,线程必 ...

  3. java线程学习之yield方法

    yield方法是暂停当前正在执行的线程对象,并执行其他线程. 这是一个静态方法,一旦执行,它会使当前线程让出CPU.让出的cpu并不代表当前线程不执行了.当前线程让出CPU后,还会CPU资源的争夺,但 ...

  4. Java线程学习详解

    线程基础 1. 线程的生命周期 1.1 新建状态: 使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态.它保持这个状态直到程序 start() 这个线程. 1 ...

  5. 模拟做饭系统(java+线程中的join方法)

    (一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...

  6. 停止Java线程,小心interrupt()方法

    来源:http://blog.csdn.net/wxwzy738/article/details/8516253 程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决 ...

  7. java 线程学习

    转载:详见处http://lavasoft.blog.51cto.com/62575/27069   Java多线程编程总结   下面是Java线程系列博文的一个编目:   Java线程:概念与原理 ...

  8. 学习java线程学习笔记

    线程:代码执行的一个分支          主要作用是提高了效率,cpu能同时执行多个部分的代码.      线程的创建:两种方式      a.继承于thread类,重写run方法.      b. ...

  9. JAVA线程sleep和wait方法区别

    一. sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复,调用sleep 不会释放对象锁.由于没有释放对象锁,所以不能 ...

随机推荐

  1. 使用Java Low Level REST Client操作elasticsearch

    Java REST客户端有两种风格: Java低级别REST客户端(Java Low Level REST Client,以后都简称低级客户端算了,难得码字):Elasticsearch的官方low- ...

  2. mysql字符集校对

    常用的两种 utf8_general_ci 按照普通的字母顺序,而且不区分大小写(比如:a B c D)utf8_bin 按照二进制排序(比如:A排在a前面,B D a c) ci是 case ins ...

  3. java_基础_类加载

    类加载:Java命令的作用是启动虚拟机,虚拟机通过输入流,从磁盘上将字节码文件(.class文件)中的内容读入虚拟机,并保存起来的过程就是类加载. 类加载特性 :      *在虚拟机的生命周期中一个 ...

  4. iOS UIView 选择性倒角

    有些APP中会有卡券,卡券做成了选择性倒角,例如左上,右上倒角.非常美观.看一下iOS的实现: #import "Masonry.h" @interface WJWDaojiaoV ...

  5. jmeter 之 beanshell sample

    Lightweight Scripting for Java 官网:http://www.BeanShell.org/ 定时器: BeanShell Timer 前置处理器:BeanShell Pre ...

  6. RoR - Restful Actions

    Index: 用于检索所有条目 # index.json.jbuilder json.array!(@post) do |post| json.extract! post, :id, :title, ...

  7. html转markdown网站

    戳下面的链接,可以直接复制富文本粘贴编程markdown: https://euangoddard.github.io/clipboard2markdown/

  8. asp.net 使用rabbitmq事例

    本例asp.net 使用rabbitmq需求背景:为了提升用户体验,用户点击下单按钮,后台先做一些简单必要的操作,返回给用户一个友好提示(比如提示处理中,或者订单状态为处理中),然后发通过发消息给队列 ...

  9. liunx安装py.27

    liunx安装py.27 按网站(https://blog.csdn.net/u012071918/article/details/78817344) 上的教程安装py.27 1.安装依赖的库 在终端 ...

  10. canal 代码阅读

    涉及到有边界队列,无边界队列.poolSize.corePoolSize.maximumPoolSize 三者参数含义 If there are more than corePoolSize but ...