java中wait()和sleep()的区别

一、wait和sleep的相同点

  1. 都可以使线程休眠
  2. 都可以响应interrupt的相应

二、wait和sleep的区别

wait和sleep在参数传递上大致相同,但传递值为0时,就是天壤之别,看下面的代码:

public class Demo8 {
    public static void main(String[] args) {
        Thread thread1=new Thread(()->{
            System.out.println("线程一开始启动");
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程一执行完成");
        });
        Object o=new Object();
        Thread thread2=new Thread(()->{
            System.out.println("线程二开始执行");
            synchronized (o){
                try {
                    o.wait(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程二执行完成");
        });
        thread1.start();
        thread2.start();
    }
}

执行结果是这样的,我们可以看出,线程二会一直等待下去,而且wait必须搭配synchronized使用
在这里插入图片描述
总结:

  1. wait必须搭配synchronized使用,sleep不用
  2. sleep是Thread的方法,wait是Object的方法
  3. sleep不释放锁,wait释放锁(见下面的代码)
  4. sleep有明确的终止等待时间,但是wait有可能一直等待下去
  5. sleep的线程状态为TIMED_WAITING,而wait是WAITING状态
public class Demo9 {
    public static void main(String[] args) throws InterruptedException {
        Object object1=new Object();
        Object object2=new Object();
        Thread thread1=new Thread(()->{
            synchronized (object1) {
                System.out.println("线程一开始启动");
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程一结束");
            }
        });
        Thread thread2=new Thread(()->{
            synchronized (object2){
                System.out.println("线程二开始执行");
                try {
                    object2.wait(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程二结束");
            }
        });
        thread1.start();
        thread2.start();
        Thread thread3=new Thread(()->{
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (object1){
                System.out.println("获取到sleep的锁");
            }
        });
        Thread thread4=new Thread(()->{
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (object2){
                System.out.println("获取到wait的锁");
            }
        });
        thread3.start();
        thread4.start();
    }
}

这段代码的含义是:创建两个线程,并对两个对象加锁,在这两个线程中分别使用wait和sleep方法,并将等待时间大于第三个第四个线程,接下来使用线程三、四,分别访问前两个线程中上锁的对象,若能在一二线程执行期间访问到,说明当时锁已经释放。
在这里插入图片描述
我们可以看到,sleep的锁是等线程执行结束后才能访问,这也就说明wait释放锁,sleep不释放锁。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

友农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值