Java多线程通信Park和Unpark,解决了什么问题

Park和Unpark介绍

park

Disables the current thread for thread scheduling purposes unless the permit is available.

  • 让当前线程不可行,等待获取许可证,除非被当前线程unpark方法调用。

unpark

Makes available the permit for the given thread, if it was not already available. If the thread was blocked on park then it will unblock. Otherwise, its next call to 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.

  • 可以让不可行的线程(调用park的线程)可继续执行。

Park和Unpark解决的问题

可以提前给线程允许执行,在线程park之前,先unpark,线程不会阻塞。对比wait和notify,如果notify先执行,wait后执行,产生阻塞状态。

代码示例

通过两个线程通信,producer发送消息,consumer接受消息。

示例一

通过wait和notify实现,producer线程在consumer线程之前,先notify,程序执行后,一直在等待接受消息。

 public void testWaitAndNotifyDeadBlockOfExecteOrder() {

        Thread consumer = new Thread(() -> {

            while (true) {
                while (message == null) {
                    System.out.println("等待接受消息");
                    try {
                        Thread.sleep(3000);
                        synchronized (this) {
                            this.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("接受消息 => " + message);
                message = null;

            }
        });

        consumer.start();

        Thread producer = new Thread(() -> {
            try {
                Thread.sleep(2000);
                message = "Hello , this is " + i++;

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (this) {
                this.notifyAll();
            }
        });

        producer.start();
    }

示例二

通过park和unpark实现,同样是在producer先执行。但是consumer依然能接受到消息。

  public void testParkAndUnPark() {
        Thread consumer = new Thread(() -> {

            while (message == null) {
                System.out.println("等待接受消息");
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LockSupport.park();
            }
            System.out.println("接受消息 => " + message);
            message = null;
        });

        consumer.start();

        Thread producer = new Thread(() -> {
            try {
                Thread.sleep(3000);
                message = "Hello , this is " + i++;
                LockSupport.unpark(consumer);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (this) {
                this.notifyAll();
            }

        });

        producer.start();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱折腾的Albert

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

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

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

打赏作者

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

抵扣说明:

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

余额充值