Java
Java多线程

Java多线程 05 - 线程休眠

简介:sleep()定义在Thread.java中。sleep()的作用是让当前线程休眠,即当前线程会从运行状态进入到休眠(阻塞)状态。sleep()会指定休眠时间,线程休眠的时间会大于 / 等于该休眠时间;在线程重新被唤醒时,它会由阻塞状态变成就绪状态,从而等待CPU的调度执行。

1. sleep()介绍

sleep()定义在Thread.java中。sleep()的作用是让当前线程休眠,即当前线程会从运行状态进入到休眠(阻塞)状态。sleep()会指定休眠时间,线程休眠的时间会大于 / 等于该休眠时间;在线程重新被唤醒时,它会由阻塞状态变成就绪状态,从而等待CPU的调度执行。

下面是一个简单的sleep()示例:

  • package com.coderap.sleep;
  • public class SleepTest {
  • public static void main(String[] args) {
  • new Thread(new Runnable() {
  • @Override
  • public void run() {
  • for (int i = 0; i < 10; i++) {
  • System.out.println(Thread.currentThread().getName() + " " + i);
  • if (i % 3 == 0) {
  • System.out.println(Thread.currentThread().getName() + " begin sleep");
  • // 记录睡眠前的时间
  • long startSleepTime = System.currentTimeMillis();
  • try {
  • // 睡眠操作
  • Thread.sleep(3000);
  • } catch (InterruptedException e) {
  • e.printStackTrace();
  • }
  • // 计算睡眠时间
  • long endSleepTime = System.currentTimeMillis();
  • System.out.println(Thread.currentThread().getName() + " end sleep, duration: " + (endSleepTime - startSleepTime) + " ms.");
  • }
  • }
  • }
  • }).start();
  • }
  • }

运行上面的代码某一次会有如下结果:

  • Thread-0 0
  • Thread-0 begin sleep
  • Thread-0 end sleep, duration: 3005 ms.
  • Thread-0 1
  • Thread-0 2
  • Thread-0 3
  • Thread-0 begin sleep
  • Thread-0 end sleep, duration: 3005 ms.
  • Thread-0 4
  • Thread-0 5
  • Thread-0 6
  • Thread-0 begin sleep
  • Thread-0 end sleep, duration: 3000 ms.
  • Thread-0 7
  • Thread-0 8
  • Thread-0 9
  • Thread-0 begin sleep
  • Thread-0 end sleep, duration: 3004 ms.

2. sleep()与wait()的比较

wait()的作用是让当前线程由运行状态进入等待(阻塞)状态的同时,也会释放同步锁。而sleep()的作用是也是让当前线程由运行状态进入到休眠(阻塞)状态。但是,wait()会释放对象的同步锁,而sleep()则不会释放锁。下面通过示例演示sleep()是不会释放锁的。

  • class TestThread implements Runnable {
  • private Object lock;
  • public TestThread(Object lock) {
  • this.lock = lock;
  • }
  • @Override
  • public void run() {
  • synchronized (this.lock) {
  • for (int i = 1; i <= 5; i++) {
  • System.out.println(Thread.currentThread().getName() + " " + i);
  • if (i % 3 == 0) {
  • System.out.println(Thread.currentThread().getName() + " begin sleep");
  • // 记录睡眠前的时间
  • long startSleepTime = System.currentTimeMillis();
  • try {
  • // 睡眠操作
  • Thread.sleep(3000);
  • } catch (InterruptedException e) {
  • e.printStackTrace();
  • }
  • // 计算睡眠时间
  • long endSleepTime = System.currentTimeMillis();
  • System.out.println(Thread.currentThread().getName() + " end sleep, sleep duration: " + (endSleepTime - startSleepTime) + " ms.");
  • }
  • }
  • }
  • }
  • }
  • public class SleepTest {
  • public static void main(String[] args) {
  • // 创建一个共享的对象作为锁
  • Object lock = new Object();
  • new Thread(new TestThread(lock), "thread-1").start();
  • new Thread(new TestThread(lock), "thread-2").start();
  • }
  • }

上述代码中两个线程将共享同一把锁。某一次运行结果如下:

  • thread-1 1
  • thread-1 2
  • thread-1 3
  • thread-1 begin sleep
  • thread-1 end sleep, sleep duration: 3002 ms.
  • thread-1 4
  • thread-1 5
  • thread-2 1
  • thread-2 2
  • thread-2 3
  • thread-2 begin sleep
  • thread-2 end sleep, sleep duration: 3005 ms.
  • thread-2 4
  • thread-2 5

可以看到,在第4行thread-1进行了睡眠操作,但是并没有释放锁,thread-2线程必须等待thread-1线程执行完后才能获得被thread-1释放的锁进而执行run()方法中的代码。