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

先上代码:

 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先创建成功节点呢?

1个回答

异步是不能保证顺序的,如果你要保证顺序,就得做同步

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问