You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by nameof <gi...@git.apache.org> on 2018/11/12 07:30:28 UTC

[GitHub] zookeeper pull request #618: ZOOKEEPER-1011:fix Java Barrier Documentation e...

Github user nameof commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/618#discussion_r232445594
  
    --- Diff: zookeeper-docs/src/documentation/content/xdocs/zookeeperTutorial.xml ---
    @@ -177,58 +165,106 @@ a boolean flag that enables the process to set a watch. In the code the flag is
              * @throws KeeperException
              * @throws InterruptedException
              */
    +        boolean enter() throws Exception {
    +            boolean readyPathExists = zk.exists(readyPath, watcher) != null;
     
    -        boolean enter() throws KeeperException, InterruptedException{
    -            zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
    -                    CreateMode.EPHEMERAL_SEQUENTIAL);
    -            while (true) {
    -                synchronized (mutex) {
    -                    List&lt;String&gt; list = zk.getChildren(root, true);
    +            zk.create(ourPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    +            return readyPathExists || internalEnter();
    +        }
     
    -                    if (list.size() &lt; size) {
    -                        mutex.wait();
    -                    } else {
    -                        return true;
    +        private synchronized boolean internalEnter() throws Exception {
    +            boolean result = true;
    +            List&lt;String&gt; list = zk.getChildren(barrierPath, false);
    +            do {
    +                if (list.size() >= size) {
    +                    try {
    +                        zk.create(readyPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    +                    } catch (KeeperException.NodeExistsException ignore) {
    +                        // ignore
    +                    }
    +                    break;
    +                } else {
    +                    if (!hasBeenNotified.get()) {
    +                        wait();
                         }
                     }
    -            }
    +            } while (false);
    --- End diff --
    
    considering spurious wakeup, you should always use ```while(true)``` here, or change ```if (!hasBeenNotified.get())``` to ```while (!hasBeenNotified.get())``` 
    https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--


---