Make waitForMultipleFutures a top-level function instead of extension
This commit is contained in:
committed by
Nikolay Igotti
parent
ee42edc9a5
commit
901219ed5f
@@ -29,7 +29,7 @@ fun testRandomWorkers() {
|
||||
// Now collect all results into current attempt's list
|
||||
val futureSet = futures.toSet()
|
||||
for (i in 0 until futureSet.size) {
|
||||
val ready = futureSet.waitForMultipleFutures(10000)
|
||||
val ready = waitForMultipleFutures(futureSet, 10000)
|
||||
ready.forEach { results[attempt].addAll(it.result) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ fun initJobs(count: Int) = Array<Job?>(count) { i -> Job(i, i * 2, i)}
|
||||
val futureSet = futures.toSet()
|
||||
var consumed = 0
|
||||
while (consumed < futureSet.size) {
|
||||
val ready = futureSet.waitForMultipleFutures(10000)
|
||||
val ready = waitForMultipleFutures(futureSet, 10000)
|
||||
ready.forEach {
|
||||
it.consume { job ->
|
||||
assertEquals(job.index * 3, job.counter)
|
||||
|
||||
@@ -29,7 +29,7 @@ data class WorkerResult(val intResult: Int, val stringResult: String)
|
||||
val futureSet = futures.toSet()
|
||||
var consumed = 0
|
||||
while (consumed < futureSet.size) {
|
||||
val ready = futureSet.waitForMultipleFutures(10000)
|
||||
val ready = waitForMultipleFutures(futureSet, 10000)
|
||||
ready.forEach {
|
||||
it.consume { result ->
|
||||
if (result.stringResult != "attempt $attempt result") throw Error("Unexpected $result")
|
||||
|
||||
@@ -63,28 +63,33 @@ public class Future<T> internal constructor(val id: Int) {
|
||||
override public fun toString(): String = "future $id"
|
||||
}
|
||||
|
||||
|
||||
@Deprecated("Use 'waitForMultipleFutures' top-level function instead", ReplaceWith("waitForMultipleFutures(this, millis)"), DeprecationLevel.ERROR)
|
||||
public fun <T> Collection<Future<T>>.waitForMultipleFutures(millis: Int): Set<Future<T>> = waitForMultipleFutures(this, millis)
|
||||
|
||||
|
||||
/**
|
||||
* Wait for availability of futures in the collection. Returns set with all futures which have
|
||||
* value available for the consumption, i.e. [FutureState.COMPUTED]
|
||||
* value available for the consumption, i.e. [FutureState.COMPUTED].
|
||||
*
|
||||
* @param millis the amount of time to wait for the computed future
|
||||
* @param timeoutMillis the amount of time in milliseconds to wait for the computed future
|
||||
*/
|
||||
public fun <T> Collection<Future<T>>.waitForMultipleFutures(millis: Int): Set<Future<T>> {
|
||||
public fun <T> waitForMultipleFutures(futures: Collection<Future<T>>, timeoutMillis: Int): Set<Future<T>> {
|
||||
val result = mutableSetOf<Future<T>>()
|
||||
|
||||
while (true) {
|
||||
val versionToken = versionToken()
|
||||
for (future in this) {
|
||||
for (future in futures) {
|
||||
if (future.state == FutureState.COMPUTED) {
|
||||
result += future
|
||||
}
|
||||
}
|
||||
if (result.isNotEmpty()) return result
|
||||
|
||||
if (waitForAnyFuture(versionToken, millis)) break
|
||||
if (waitForAnyFuture(versionToken, timeoutMillis)) break
|
||||
}
|
||||
|
||||
for (future in this) {
|
||||
for (future in futures) {
|
||||
if (future.state == FutureState.COMPUTED) {
|
||||
result += future
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ fun main() {
|
||||
val futureSet = futures.toSet()
|
||||
var consumed = 0
|
||||
while (consumed < futureSet.size) {
|
||||
val ready = futureSet.waitForMultipleFutures(10000)
|
||||
val ready = waitForMultipleFutures(futureSet, 10000)
|
||||
ready.forEach {
|
||||
it.consume { result ->
|
||||
if (result.stringResult != "attempt $attempt result") throw Error("Unexpected $result")
|
||||
|
||||
Reference in New Issue
Block a user