前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LockSupport 详解

LockSupport 详解

作者头像
用户5927264
发布2021-03-22 10:10:33
3960
发布2021-03-22 10:10:33
举报
文章被收录于专栏:OSChinaOSChina
代码语言:javascript
复制
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();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档