zoukankan      html  css  js  c++  java
  • 九、curator recipes之不可重入锁InterProcessSemaphoreMutex

    简介

    recipes的InterProcessSemaphoreMutex是一种不可重入的互斥锁,也就意味着即使是同一个线程也无法在持有锁的情况下再次获得锁,所以需要注意,不可重入的锁很容易在一些情况导致死锁,比如你写了一个递归。

    官方文档:http://curator.apache.org/curator-recipes/shared-lock.html

    javaDoc:http://curator.apache.org/curator-recipes/shared-lock.html

    代码示例

    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    public class SemaphoreMutexDemo {
        private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 3));
        private static String path = "/locks/semaphore/0001";
        static {
            client.start();
        }
    
        public static void main(String[] args) throws Exception {
            startThread0();
            Thread.sleep(10);
            startThread1();
            Thread.sleep(50000);
            client.close();
        }
    
        private static void startThread0() {
            new Thread(() -> {
                InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(client, path);
                try {
                    System.out.println("thread0 acquiring");
                    lock.acquire();
                    System.out.println("thread0 acquired");
                    System.out.println("thread0 sleep for 3s");
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        lock.release();
                        System.out.println("thread0 release");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    
        private static void startThread1() {
            new Thread(() -> {
                InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(client, path);
                try {
                    System.out.println("thread1 acquiring");
                    lock.acquire();
                    System.out.println("thread1 acquired");
                    System.out.println("thread1 sleep for 3s");
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        lock.release();
                        System.out.println("thread1 release");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
  • 相关阅读:
    sql 保存,性能高
    老生常谈-从输入url到页面展示到底发生了什么
    HTTP 协议入门
    理解HTTP协议
    利用Vue v-model实现一个自定义的表单组件
    浅谈 Vue v-model指令的实现原理
    vuex介绍--一篇看懂vuejs的状态管理神器
    ES6之 =>箭头函数
    position定位-absolute与fixed
    JavaScript:理解事件、事件处理函数、钩子函数、回调函数
  • 原文地址:https://www.cnblogs.com/lay2017/p/10274906.html
Copyright © 2011-2022 走看看