package com.kwan.shuyu; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.apache.curator.retry.ExponentialBackoffRetry; import java.util.concurrent.TimeUnit; /** * 12306买票 * * @author : qinyingjie * @version : 2.2.0 * @date : 2023/5/14 19:17 */ public class Ticket12306 implements Runnable { /** * 数据库的票数 */ private int tickets = 10; private InterProcessMutex lock; public Ticket12306() { //重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); //2.第二种方式 //CuratorFrameworkFactory.builder(); CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.149.135:2181") .sessionTimeoutMs(60 * 1000) .connectionTimeoutMs(15 * 1000) .retryPolicy(retryPolicy) .build(); //开启连接 client.start(); lock = new InterProcessMutex(client, "/lock"); } @Override public void run() { while (true) { //获取锁 try { lock.acquire(3, TimeUnit.SECONDS); if (tickets > 0) { System.out.println(Thread.currentThread() + ":" + tickets); Thread.sleep(100); tickets--; } } catch (Exception e) { e.printStackTrace(); } finally { //释放锁 try { lock.release(); } catch (Exception e) { e.printStackTrace(); } } } } }