• 详解LockSupport的使用


    一、简介

    1.下图是Java api中的描述内容:
    在这里插入图片描述

    2.它的方法有以下几个,其中被圈出来的方法是核心方法:
    在这里插入图片描述
    3.唤醒和阻塞线程的常用方法中包含了LockSupport:
    在这里插入图片描述

    二、LockSupport唤醒阻塞方法的优势

    1.Object类提供的waitnotify需要在synchronized代码块中使用,否则会报IllegalMonitorStateException异常 !并且notify必须在wait方法后面执行才能够生效 。(下面是正确的用法!)

        public static void main(String[] args) {
            Object objectlock = new Object();
            new Thread(()->{
                synchronized (objectlock){
                    System.out.println(Thread.currentThread().getName()+":\tcome in~");
                    try {objectlock.wait();} catch (InterruptedException e) {e.printStackTrace();}
                    System.out.println(Thread.currentThread().getName()+": \t被唤醒");
                }
            },"A").start();
    		//休眠1秒
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
    
            new Thread(()->{
                synchronized (objectlock) {
                    objectlock.notify();
                    System.out.println(Thread.currentThread().getName()+": \t发出通知!");
                }
            },"B").start();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.ReentrantLock类提供的awaitsignal依旧存在执行顺序的问题,下面的案例让signal方法先执行,结果发现后执行的await并没有被唤醒!

    public static void main(String[] args) {
            Lock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
            new Thread(()->{
            	//暂停1秒,让B线程先执行!
                try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+":\tcome in~");
                    condition.await();
                    System.out.println(Thread.currentThread().getName()+": \t被唤醒");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            },"A").start();
    
            new Thread(()->{
                lock.lock();
                try {
                    condition.signal();
                    System.out.println(Thread.currentThread().getName()+": \t发出通知!");
                }finally {
                    lock.unlock();
                }
            },"B").start();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述
    3.LockSupport就不存在上面的问题,类似高速公路的ETC,提前买好了通行证unpark,到闸机处直接抬起栏杆放行了没有park拦截了。但unpark重复执行也只能积累一次通行证,所以下面的代码如果将注释掉的部分都去掉,那么A线程就会被阻塞!

        public static void main(String[] args) {
            Thread a = new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"休眠1s");
                try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
                LockSupport.park();
                //LockSupport.park();
                System.out.println(Thread.currentThread().getName()+"解除阻塞成功!!!");
            }, "A");
            a.start();
    
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"发放通行证!!");
                LockSupport.unpark(a);
                //LockSupport.unpark(a);
            },"B").start();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

  • 相关阅读:
    Antv G6入门之旅--combo图
    ReentrantLock源码剖析
    Verilog实现SPI通信协议驱动设计
    【C++】vector的认识+模拟实现
    Python:实现lorenz transformation 洛伦兹变换算法(附完整源码)
    计算机网络复习-第六章应用层
    day60
    C++内存泄露
    EluxJS-让你像切蛋糕一样拆解前端巨石应用
    5大负载均衡算法 (原理图解)
  • 原文地址:https://blog.csdn.net/MoastAll/article/details/125495380