目录

一、wait VS sleep

二、wait(0) VS sleep(0)

一、wait VS sleep

1. 相同点

① 都可以让线程进入休眠状态。

② 都可以相应interrupt中断请求。

响应中断请求:

public class WaitSleepDemo {
    public static void main(String[] args) throws InterruptedException {
        Object lock=new Object();
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("线程1:开始执行");
                try {
                    lock.wait(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1:结束执行");
            }
            System.out.println("线程1:终止执行");
        });
        t1.start();
        Thread.sleep(100);
        System.out.println("执行线程1的终止方法");
        t1.interrupt();

        Thread t2=new Thread(()-> {
            System.out.println("线程2:开始执行");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2:结束执行");
        });
        t2.start();
        
        Thread.sleep(100);
        System.out.println("执行线程2的终止方法");
        t2.interrupt();
    }
}

  

2. 不同点

① wait必须配合synchronized使用,而sleep不用;

② wait属于Object(对象)的方法,而sleep属于Thread(线程)的方法;

③ sleep不释放锁,而wait要释放锁;

④ sleep必须要传递一个数值型的参数,而wait可以不传参;

⑤ sleep让线程进入到TIMED_WAITING状态,而无参的wait方法让线程进入了WAITING状态;

⑥ 一般情况下,sleep只能等待超过时间之后才能恢复执行,而wait可以接收notify、notifyAll之后就可以执行。

wait和sleep释放锁情况:

import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * wait和sleep释放锁的区别
 */
public class WaitSleepDemo2 {
    public static void main(String[] args) {
        Object lock=new Object();
        Object lock2=new Object();
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("线程1:开始执行");
                try {
                    lock.wait(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1:结束执行");
            }
            System.out.println("线程1:终止执行");
        }, "wait()");
        t1.start();


        Thread t2=new Thread(()-> {
            synchronized (lock2){
                System.out.println("线程2:开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2:结束执行");
            }
        },"sleep()");
        t2.start();
        //创建两个线程,先让线程休眠1s之后,尝试获取,看能不能获取到锁
        //如果可以获取到锁,说明休眠时线程时释放锁的,而如果获取不到锁,说明线程是不释放锁
        Thread t3=new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("尝试获取wait方法的锁");
            synchronized (lock){
                System.out.println("获取wait方法的锁成功");
            }
        });
        t3.start();

        Thread t4=new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("尝试获取sleep方法的锁");
            synchronized (lock2){
                System.out.println("获取sleep方法的锁成功");
            }
        });
        t4.start();
    }
}

由此说明,wait会释放锁,而sleep不释放锁。

二、wait(0) VS sleep(0)

-  wait(0) 会一直休眠等待下去,直到notify/notifyAll方法唤醒它。

- sleep(0)相当于Thread.yield(),让出CPU执行权重新调度,但是sleep(0)会继续执行。

public class WaitSleepDemo {
    public static void main(String[] args) throws InterruptedException {
        Object lock=new Object();
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("线程1:开始执行");
                try {
                    lock.wait(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1:结束执行");
            }
            System.out.println("线程1:终止执行");
        });
        t1.start();


        Thread t2=new Thread(()-> {
            System.out.println("线程2:开始执行");
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2:结束执行");
        });
        t2.start();

    }
}

 

Logo

鸿蒙生态一站式服务平台。

更多推荐