zoukankan      html  css  js  c++  java
  • 十、curator recipes之信号量InterProcessSemaphoreV2

    简介

    跟Java并信号量没有什么不同,curator实现的信号量也是基于令牌桶算法,当一个线程要执行的时候就去桶里面获取令牌,如果有足够的令牌那么我就执行如果没有那么我就阻塞,当线程执行完毕也要将令牌放回桶里。

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

    javaDoc:http://curator.apache.org/apidocs/org/apache/curator/framework/recipes/locks/InterProcessSemaphoreV2.html

    代码示例

    以下示例中,我们设置了信号量为1,如果其中一个线程取走了,那么下一个线程将阻塞直接信号量被返回到桶里面。

    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2;
    import org.apache.curator.framework.recipes.locks.Lease;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    public class InterProcessSemaphoreDemo {
        private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 2));
        private static String path = "/semaphore/0001";
        static {
            client.start();
        }
    
        public static void main(String[] args) throws InterruptedException {
            startThread0();
            startThread1();
            Thread.sleep(50000);
            client.close();
        }
    
        private static void startThread1() {
            new Thread(() -> {
                InterProcessSemaphoreV2 semaphoreV2 = new InterProcessSemaphoreV2(client, path, 1);
                Lease lease = null;
                try {
                    System.out.println("thread0 acquiring");
                    lease = semaphoreV2.acquire();
                    System.out.println("thread0 acquired and sleeping");
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    semaphoreV2.returnLease(lease);
                    System.out.println("thread0 return lease");
                }
            }).start();
        }
    
        private static void startThread0() {
            new Thread(() -> {
                InterProcessSemaphoreV2 semaphoreV2 = new InterProcessSemaphoreV2(client, path, 1);
                Lease lease = null;
                try {
                    System.out.println("thread1 acquiring");
                    lease = semaphoreV2.acquire();
                    System.out.println("thread1 acquired and sleeping");
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    semaphoreV2.returnLease(lease);
                    System.out.println("thread1 return lease");
                }
            }).start();
        }
    }
  • 相关阅读:
    近期前端中的 一些常见的面试题
    一道前端学习题
    前端程序员容易忽视的一些基础知识
    web前端工程师入门须知
    Web前端知识体系精简
    面试分享:一年经验初探阿里巴巴前端社招
    抽象类、抽象函数/抽象方法详解
    C#语法-虚方法详解 Virtual 虚函数
    面向对象语言:继承关系教程
    C#动态创建Xml-LinQ方式
  • 原文地址:https://www.cnblogs.com/lay2017/p/10274948.html
Copyright © 2011-2022 走看看