sleep和wait的区别

1.sleep()是Thread类的方法,是native类型的。wait()是Object类中定义的方法,也是native类型的。

2.sleep()方法可以再任何地方使用。wait()方法只能在synchronized或者synchronized块中使用。因为synchronized是用来加锁,wait是用来释放锁。

两者最本质的区别:

(1)Thread.sleep只会让出CPU,不会导致锁行为的改变。

(2)Object.wait不仅会让出CPU,还会释放已经占有的同步资源锁,以便其他正在等待该资源的线程得到该资源进而去运行。

1.下面的代码表示A线程调用了wait()方法后会释放同步资源锁,释放了同步锁之后就可以供其他线程来占有这个同步资源锁。
public class WaitSleepDemo {
    public static void main(String[] args) {
        final Object lock = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread A is waiting to get lock");
                synchronized(lock){
                    try {
                        System.out.println("thread A get lock");
                        Thread.sleep(20);
                        System.out.println("thread A do wait method");
                        lock.wait(1000);
                        System.out.println("thread A is done");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        //为了让A、B线程先后执行,这里让主线程休眠10毫秒
        try{
            Thread.sleep(10);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread B is waiting to get lock");
                synchronized(lock){
                    try {
                        System.out.println("thread B get lock");
                        System.out.println("thread B is sleeping");
                        Thread.sleep(10);
                        System.out.println("thread B is done");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
运行结果:
thread A is waiting to get lock
thread A get lock
thread B is waiting to get lock
thread A do wait method
thread B get lock
thread B is sleeping
thread B is done
thread A is done

 

2.接下来这个例子说明,sleep是没有改变锁的行为的。这里执行了sleep,但是没有把锁释放,所以B线程无法获取同步锁,直到A执行完后才能获取。
public class WaitSleepDemo {
    public static void main(String[] args) {
        final Object lock = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread A is waiting to get lock");
                synchronized(lock){
                    try {
                        System.out.println("thread A get lock");
                        Thread.sleep(20);
                        System.out.println("thread A do wait method");
                        Thread.sleep(1000);
                        System.out.println("thread A is done");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        //为了让A、B线程先后执行,这里让主线程休眠10毫秒
        try{
            Thread.sleep(10);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread B is waiting to get lock");
                synchronized(lock){
                    try {
                        System.out.println("thread B get lock");
                        System.out.println("thread B is sleeping");
                        lock.wait(10);
                        System.out.println("thread B is done");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
运行结果:
thread A is waiting to get lock
thread A get lock
thread B is waiting to get lock
thread A do wait method
thread A is done
thread B get lock
thread B is sleeping
thread B is done

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一心只敲圣贤码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值