Kotlin TimeoutException
Future.get can bound how long checkout waits for a pending quote.
A simple example
TimeoutException
- Start with An unfinished task
- Do this Wait only until a deadline
- Result Time limit reached
Syntax
quote.get(0, TimeUnit.SECONDS)
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarLimit the wait for a shipping quote
A checkout screen has used its entire waiting budget for a carrier quote. The quote is still pending, so the customer should see a pending message while the underlying work remains available.
import java.util.concurrent.TimeUnit
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeoutException
fun main() {
val quote = CompletableFuture<Int>()
try {
println(quote.get(0, TimeUnit.SECONDS))
} catch (problem: TimeoutException) {
println("Quote pending")
}
}
Quote pending
There is no result within the zero-length wait, so checkout reports a pending quote immediately.
Poll a finished download
A document download has already completed when the kiosk checks. Inspect the job using a zero-wait poll. A job that has yet to complete stays in the waiting queue.
Poll a pending proof
A designer checks a pending proof without blocking the screen. Inspect the job using a zero-wait poll. A job that has yet to complete stays in the waiting queue.
What to watch for
The timeout applies to this wait. It leaves completion or cancellation of the future to its owner.