LockSupport 详解

原创
03/17 14:08
阅读数 95

 

package com.shi.flink.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author shiye
 * @create 2021-03-17 10:03
 */
public class LocalSupportTest {

    public static void main(String[] args) {
        lockSupportTest();
    }

    /**
     * LockSupport 测试
     */
    private static void lockSupportTest() {
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "  ---->> commint in .. ");
            //必须要获得凭证才可以通行
            LockSupport.park();
            System.out.println("------获得了一个凭证-----");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "  ---->> 被唤醒 .. ");
        });
        thread1.start();

        /**
         * 给同一个线程发放多次凭证,只会有一个凭证有效
         */
        new Thread(()->{
            //给 thread1 发送凭证
            LockSupport.unpark(thread1);
            System.out.println("------给thread1发送了1个凭证-----");
            LockSupport.unpark(thread1);
            System.out.println("------给thread1发送了第2个凭证-----");
            System.out.println(Thread.currentThread().getName() + "  ---->> 通知/唤醒了别得线程 .. ");
        }).start();
    }

    /**
     * 1.必须要保证顺序
     * 2.必须要加锁
     */
    private static void lockTest2() {
        Lock lock = new ReentrantLock();

        Thread t1 = new Thread(()->{
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "  ---->> commint in .. ");
                Thread.currentThread().wait();
                System.out.println(Thread.currentThread().getName() + "  ---->> 被唤醒 .. ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        t1.start();

        Thread t2 = new Thread(()->{
            try {
                lock.lock();
                t1.notify();
                System.out.println(Thread.currentThread().getName() + "  ---->> 通知/唤醒了别得线程 .. ");
            } finally {
                lock.unlock();
            }
        });
        t2.start();
    }

    /**
     * 1.必须要保证顺序
     * 2.必须要加锁
     */
    private static void synchronizedLock1() {
        Object lockObject = new Object();

        new Thread(()->{
            //如果之前别的线程去提前去唤醒当前线程,则无效
//            try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }

            synchronized (lockObject){
                System.out.println(Thread.currentThread().getName() + "  ---->> commint in .. ");
                try {
                    lockObject.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "  ---->> 被唤醒 .. ");
            }
        }).start();

        new Thread(()->{
            synchronized (lockObject){
                lockObject.notify();
                System.out.println(Thread.currentThread().getName() + "  ---->> 通知/唤醒了别得线程 .. ");
            }
        }).start();
    }
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部