Kotlin Tutorials

Kotlin Set waitForMultipleFutures()
Syntax & Examples

Set.waitForMultipleFutures() extension function

The waitForMultipleFutures() extension function in Kotlin waits for multiple futures to complete within a specified timeout.


Syntax of Set.waitForMultipleFutures()

The syntax of Set.waitForMultipleFutures() extension function is:

fun <T> Collection<Future<T>>.waitForMultipleFutures(millis: Int): Set<Future<T>>

This waitForMultipleFutures() extension function of Set waits for multiple futures to complete within the given timeout in milliseconds.

Parameters

ParameterOptional/RequiredDescription
millisrequiredThe timeout in milliseconds within which the futures should complete.

Return Type

Set.waitForMultipleFutures() returns value of type Set>.



✐ Examples

1 Waiting for multiple futures to complete within 1000 milliseconds

Using waitForMultipleFutures() to wait for multiple futures to complete within 1000 milliseconds.

For example,

  1. Create a set of futures.
  2. Use waitForMultipleFutures() with a timeout of 1000 milliseconds to wait for the futures to complete.
  3. Print the resulting set of completed futures.

Kotlin Program

import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.Future

fun main() {
    val executor = Executors.newFixedThreadPool(3)
    val futures = setOf(
        executor.submit(Callable { Thread.sleep(500); 1 }),
        executor.submit(Callable { Thread.sleep(1500); 2 }),
        executor.submit(Callable { Thread.sleep(1000); 3 })
    )
    val completedFutures = futures.waitForMultipleFutures(1000)
    println(completedFutures.map { it.get() })
    executor.shutdown()
}

Output

[1, 3]

2 Waiting for multiple futures to complete within 500 milliseconds

Using waitForMultipleFutures() to wait for multiple futures to complete within 500 milliseconds.

For example,

  1. Create a set of futures.
  2. Use waitForMultipleFutures() with a timeout of 500 milliseconds to wait for the futures to complete.
  3. Print the resulting set of completed futures.

Kotlin Program

import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.Future

fun main() {
    val executor = Executors.newFixedThreadPool(3)
    val futures = setOf(
        executor.submit(Callable { Thread.sleep(300); 1 }),
        executor.submit(Callable { Thread.sleep(700); 2 }),
        executor.submit(Callable { Thread.sleep(400); 3 })
    )
    val completedFutures = futures.waitForMultipleFutures(500)
    println(completedFutures.map { it.get() })
    executor.shutdown()
}

Output

[1, 3]

3 Waiting for multiple futures to complete with a custom timeout

Using waitForMultipleFutures() to wait for multiple futures to complete with a custom timeout.

For example,

  1. Create a set of futures.
  2. Use waitForMultipleFutures() with a custom timeout to wait for the futures to complete.
  3. Print the resulting set of completed futures.

Kotlin Program

import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.Future

fun main() {
    val executor = Executors.newFixedThreadPool(3)
    val futures = setOf(
        executor.submit(Callable { Thread.sleep(200); 1 }),
        executor.submit(Callable { Thread.sleep(600); 2 }),
        executor.submit(Callable { Thread.sleep(400); 3 })
    )
    val completedFutures = futures.waitForMultipleFutures(450)
    println(completedFutures.map { it.get() })
    executor.shutdown()
}

Output

[1, 3]

Summary

In this Kotlin tutorial, we learned about waitForMultipleFutures() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.