• 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,不会导致锁行为的改变

  • 相关阅读:
    端口扫描技术
    HBase——常用命令
    RabbitMQ——常用命令
    RabbitMQ——基于 KeepAlived + HAProxy 搭建 RabbitMQ 高可用负载均衡集群
    RabbitMQ——安装、集群搭建、镜像队列配置
    Zookeeper——常用命令
    MFC子对话框嵌入主对话框
    Find a way
    Avoid The Lakes
    变形课
  • 原文地址:https://www.cnblogs.com/linlf03/p/12113119.html
Copyright © 2020-2023  润新知