浅析Lock工具类LockSupport

LockSupport是一个编程工具类,主要是为了阻塞和唤醒线程用的,所有的方法都是静态方法,可以让线程在任意位置阻塞,也可以在任意位置唤醒。使用它我们可以实现很多功能,今天主要就是对这个工具类的讲解,希望对你有帮助。


LockSupport简介

在介绍java中的Lock时,比如ReentrantLock,ReentReadWriteLocks,已经在介绍线程间等待/通知机制使用的Condition时都会调用LockSupport.park()方法和LockSupport.unpark()方法。而这个在同步组件的实现中被频繁使用的LockSupport到底是何方神圣,现在就来看看。LockSupprot是线程的阻塞原语,用来阻塞线程和唤醒线程。每个使用LockSupport的线程都会与一个许可关联,如果该许可可用,并且可在线程中使用,则调用park()将会立即返回,否则可能阻塞。如果许可尚不可用,则可以调用 unpark 使其可用。但是注意许可不可重入,也就是说只能调用一次park()方法,否则会一直阻塞。


LockSupport方法

LockSupport中的方法

在这里插入图片描述


LockSupport方法介绍

LockSupport中的方法不多,且都为静态方法,这里将这些方法做一个总结:

阻塞线程

  1. void park():阻塞当前线程,如果调用unpark方法或者当前线程被中断,从能从park()方法中返回
  2. void park(Object blocker):功能同方法1,入参增加一个Object对象,用来记录导致线程阻塞的阻塞对象,方便进行问题排查;
  3. void parkNanos(long nanos):阻塞当前线程,最长不超过nanos纳秒,增加了超时返回的特性;
  4. void parkNanos(Object blocker, long nanos):功能同方法3,入参增加一个Object对象,用来记录导致线程阻塞的阻塞对象,方便进行问题排查;
  5. void parkUntil(long deadline):阻塞当前线程,知道deadline;
  6. void parkUntil(Object blocker, long deadline):功能同方法5,入参增加一个Object对象,用来记录导致线程阻塞的阻塞对象,方便进行问题排查;

唤醒线程

  1. void unpark(Thread thread):唤醒处于阻塞状态的指定线程

LockSupport源码

park()源码

/**
     * Disables the current thread for thread scheduling purposes unless the
     * permit is available.
     *
     * <p>If the permit is available then it is consumed and the call
     * returns immediately; otherwise the current thread becomes disabled
     * for thread scheduling purposes and lies dormant until one of three
     * things happens:
     *
     * <ul>
     *
     * <li>Some other thread invokes {@link #unpark unpark} with the
     * current thread as the target; or
     *
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread; or
     *
     * <li>The call spuriously (that is, for no reason) returns.
     * </ul>
     *
     * <p>This method does <em>not</em> report which of these caused the
     * method to return. Callers should re-check the conditions which caused
     * the thread to park in the first place. Callers may also determine,
     * for example, the interrupt status of the thread upon return.
     */
    public static void park() {
        UNSAFE.park(false, 0L);
    }

unpark()源码

 /**
     * Makes available the permit for the given thread, if it
     * was not already available.  If the thread was blocked on
     * {@code park} then it will unblock.  Otherwise, its next call
     * to {@code park} is guaranteed not to block. This operation
     * is not guaranteed to have any effect at all if the given
     * thread has not been started.
     *
     * @param thread the thread to unpark, or {@code null}, in which case
     *        this operation has no effect
     */
    public static void unpark(Thread thread) {
        if (thread != null)
            UNSAFE.unpark(thread);
    }

我们知道了实际上LockSupport阻塞和唤醒线程的功能是依赖于sun.misc.Unsafe,这是一个很底层的类,有兴趣的可以去查阅资料,比如park()方法的功能实现则是靠unsafe.park()方法


LockSupport实例

LockSupport类经典面试题:用两个线程,一个输出字母,一个输出数字,交替输出1A2B3C4D5E…26Z

分析思路:LockSupport.park()让当前线程阻塞,LockSupport.unpark(t2)解锁t2线程。

package cn.wideth.util;

import java.util.concurrent.locks.LockSupport;

public class Main {

    private static Thread t1=null,t2=null;

    public static void main(String[] args) {

        char [] nums="123456789".toCharArray();
        char [] chars="ABCDEFGHI".toCharArray();

       t1=new Thread(()->{
            for (char c:nums) {
                System.out.print(c);
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        },"t1");

       t2=new Thread(()->{
            for (char c:chars) {
                LockSupport.park();
                System.out.print(c);
                LockSupport.unpark(t1);
            }
        },"t2");

        t1.start();
        t2.start();
    }

}

运行结果

在这里插入图片描述


本文小结

LockSupport是一个编程工具类,主要是为了阻塞和唤醒线程用的。首先介绍了LockSupport的基本概念,以及常用的方法,最后给出了LockSupport有关的一个经典面试题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值