zoukankan      html  css  js  c++  java
  • Sleep和Wait的区别

    基本区别

    1、 sleep()来自Thread, wait() 来自Object
    2、sleep可以在任何地方使用
    wait只能在synchronized方法或者synchronized块中使用 (因为wait会释放锁,所有只有获取了锁,才会释放锁)

    最主要的本质区别
    Thrad.sleep只会让出CPU,不会导致锁行为的改变
    Object.wait不仅让出CPU,还会释放已经占有的同步锁资源

    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();
    
    
            //为了让Thread A 先于Thread B执行
            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 10 ms");
                            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 10 ms
    thread B is done
    thread A is done
    

      上面的代码验证了: Object.wait不仅让出CPU,还会释放已经占有的同步锁资源 

    调整后的代码

    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);
                            Thread.sleep(1000);
                            System.out.println("thread A is done");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    
    
            //为了让Thread A 先于Thread B执行
            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 10 ms");
                           // Thread.sleep(10);
                            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 10 ms
    thread B is done
    

      上面的代码验证了:Thrad.sleep只会让出CPU,不会导致锁行为的改变

  • 相关阅读:
    记一次oracle新建用户及分配指定表权限的操作记录
    [转]word中不显示mathtype公式,只显示方框,双击后可以再mathtype里面看到公式
    C、C++成员变量对齐
    include头文件:C++标准库与C标准库
    [转]本专业部分国际会议及刊物影响因子排名
    使用Winbase.h
    [转]printf 字符串格式化
    1.6.2如何使用位逻辑运算(例如与、或、位移)来实现位向量?
    文章中图表自动编号
    取样问题 总数n事先不知道,等概率取样 (编程珠玑chapter12 课后题10)
  • 原文地址:https://www.cnblogs.com/linlf03/p/12113119.html
Copyright © 2011-2022 走看看