Repository navigation
Conversation
Adds a new private watcher class for looking for Changed Children. This resolves the Naked Notify warning and the var outside of scope error.
| private final String path; | ||
| private final ReentrantLock lock = new ReentrantLock(); | ||
| private final Condition childrenChanged = lock.newCondition(); | ||
| private boolean changed = false; |
There was a problem hiding this comment.
This should be volatile because the ZooKeeper client thread is going to call process and the application thread is calling waitForChange.
| lock.lock(); | ||
| try { | ||
| long nanos = unit.toNanos(timeout); | ||
| while (!changed && nanos > 0) { | ||
| nanos = childrenChanged.awaitNanos(nanos); | ||
| } | ||
| // Set the flag back to false so we can wait for the next change event | ||
| changed = false; | ||
| } finally { | ||
| lock.unlock(); |
There was a problem hiding this comment.
The way the locking is done here changed will never be set to true during this code block because it's waiting to acquire the same lock. I think this code will always wait the entire duration instead and then the watcher thread will acquire the lock and change the boolean value.
I think this can be simplified by using a different object type - CyclicBarrier. The code here would look something like:
private final CyclicBarrier barrier = new CyclicBarrier(2);
try {
barrier.await(nanos, TimeUnit.NANOSECONDS);
barrier.reset();
} catch (TimeoutException e) {
}
Then, in the process method above, it would just call barrier.await(). So, when both threads have called await on the barrier, it's true.
There was a problem hiding this comment.
Then, in the process method above, it would just call barrier.await(). So, when both threads have called await on the barrier, it's true.
The call to barrier.await in the process method may need to be guarded. You only want to call await if the other thread is also waiting or else it will block. You could use barrier.getNumberWaiting for this and only call await if it's greater than zero.
Adds a new private watcher class for looking for Changed Children. This resolves the Naked Notify warning and the var outside of scope error.
This is a change that's included in #6486 but can be broken out as a separate change.