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();
    }

猜你喜欢

转载自blog.csdn.net/qq_36325121/article/details/108798891