Zookeeper的Curator客户端异步创建节点,没有先后顺序吗?

weixin_38051062 weixin_38051062 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先上代码: package book.chapter05.$5_4_2; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.api.BackgroundCallback; import org.apache.curator.framework.api.CuratorEvent; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; //使用Curator的异步接口 public class Create_Node_Background_Sample { static String path = "/zk-book"; static CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("domain1.book.zookeeper:2181") .sessionTimeoutMs(5000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); static CountDownLatch semaphore = new CountDownLatch(2); static ExecutorService tp = Executors.newFixedThreadPool(2); public static void main(String[] args) throws Exception { client.start(); System.out.println("Main thread: " + Thread.currentThread().getName()); // 此处传入了自定义的Executor client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).inBackground(new BackgroundCallback() { @Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { System.out.println("One event[code: " + event.getResultCode() + ", type: " + event.getType() + "]"); System.out.println("One Thread of processResult: " + Thread.currentThread().getName()); semaphore.countDown(); } }, tp).forPath(path, "init".getBytes()); // 此处没有传入自定义的Executor client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).inBackground(new BackgroundCallback() { @Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { System.out.println("Two event[code: " + event.getResultCode() + ", type: " + event.getType() + "]"); System.out.println("Two Thread of processResult: " + Thread.currentThread().getName()); semaphore.countDown(); } }).forPath(path, "init2".getBytes()); semaphore.await(); System.out.println("通过CountDownLatch主线程一直等待,直到计数减到0"); tp.shutdown(); } } 运行结果情况(1): Main thread: main Two event[code: 0, type: CREATE] Two Thread of processResult: main-EventThread One event[code: -110, type: CREATE] One Thread of processResult: pool-3-thread-1 通过CountDownLatch主线程一直等待,直到计数减到0 运行结果情况(2): Main thread: main Two event[code: -110, type: CREATE] Two Thread of processResult: main-EventThread One event[code: 0, type: CREATE] One Thread of processResult: pool-3-thread-1 通过CountDownLatch主线程一直等待,直到计数减到0 问题内容: 请问一下,主线程中是 One先创建的节点,然后才是Two创建的节点,zookeeper对于同一客户端的会话请求不是顺序执行的吗?为什么有时候是Two先创建的节点,有时候是One先创建成功节点呢?
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部