apatrida
10/16/2015, 6:54 PMprivate class GrowableCountdownLatch {
private val growableCount = AtomicInteger(0)
private val latch = CountDownLatch(1)
public fun increment() {
growableCount.incrementAndGet()
}
public fun decrement() {
if (growableCount.decrementAndGet() == 0) {
latch.countDown()
}
}
public fun await() {
if (growableCount.get() == 0) return
latch.await()
}
}
used with the promises:
// "work" is concurrent queue
fun thatAddsWork(thing) {
latch.increment()
work.add(async {
... do work, also calls thatAddsWork()
}.always {
latch.decrement()
})
}
latch.await()
all(work.toList()).then {
...
}
it blocks in latch.await() though, if you want all non blocking this isnt' for you