Run a Callable through FutureTask and read the result after joining its thread.

future task `FutureTask` wraps a callable result so another thread can compute it and the main thread can retrieve the value later.

Future Task Result

seed
FutureTaskResult.kt
Replay: real traced execution (multi-file project)
import java.util.concurrent.Callable
import java.util.concurrent.FutureTask

fun scoreBatch(seed: Int): Int {
    val base = seed + 4
    val bonus = base / 2
    return base + bonus
}

fun main() {
    val seed = 8
    val task = FutureTask(Callable { scoreBatch(seed) })
    val worker = Thread(task)

    worker.start()
    worker.join()

    val score = task.get()
    println("seed=$seed")
    println("score=$score")
}
import java.util.concurrent.Callable
import java.util.concurrent.FutureTask

fun scoreBatch(seed: Int): Int {
    val base = seed + 4
    val bonus = base / 2
    return base + bonus
}

fun main() {
    val seed = 4
    val task = FutureTask(Callable { scoreBatch(seed) })
    val worker = Thread(task)

    worker.start()
    worker.join()

    val score = task.get()
    println("seed=$seed")
    println("score=$score")
}
import java.util.concurrent.Callable
import java.util.concurrent.FutureTask

fun scoreBatch(seed: Int): Int {
    val base = seed + 4
    val bonus = base / 2
    return base + bonus
}

fun main() {
    val seed = 12
    val task = FutureTask(Callable { scoreBatch(seed) })
    val worker = Thread(task)

    worker.start()
    worker.join()

    val score = task.get()
    println("seed=$seed")
    println("score=$score")
}
  1. seed ← 8, task ← ⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩]

    10fun main() {11    val seed→ 8 = 8 //@seed=4, 1212    val task→ ⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩] = FutureTask(Callable { scoreBatch(seed) })13    val worker→ Thread[#19,Thread-0,5,main] = Thread(task⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩])1415    workerThread[#19,Thread-0,5,main].start()16    workerThread[#19,Thread-0,5,main].join()
  2. base ← 12, bonus ← 6

    4fun scoreBatch(seed8: Int): Int {5    val base→ 12 = seed8 + 46    val bonus→ 6 = base12 / 27    return base12 + bonus68}
  3. worker ← Thread[#19,Thread-0,5,], score ← 18

    15    worker.start()16    worker→ Thread[#19,Thread-0,5,].join()1718    val score→ 18 = task⟨FutureTask B⟩[Completed normally].get()19    println("seed=$seed8")20    println("score=$score18")21}
    outputseed=8
    score=18
  1. seed ← 4, task ← ⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩]

    10fun main() {11    val seed→ 4 = 412    val task→ ⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩] = FutureTask(Callable { scoreBatch(seed) })13    val worker→ Thread[#19,Thread-0,5,main] = Thread(task⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩])1415    workerThread[#19,Thread-0,5,main].start()16    workerThread[#19,Thread-0,5,main].join()
  2. base ← 8, bonus ← 4

    4fun scoreBatch(seed4: Int): Int {5    val base→ 8 = seed4 + 46    val bonus→ 4 = base8 / 27    return base8 + bonus48}
  3. worker ← Thread[#19,Thread-0,5,], score ← 12

    15    worker.start()16    worker→ Thread[#19,Thread-0,5,].join()1718    val score→ 12 = task⟨FutureTask B⟩[Completed normally].get()19    println("seed=$seed4")20    println("score=$score12")21}
    outputseed=4
    score=12
  1. seed ← 12, task ← ⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩]

    10fun main() {11    val seed→ 12 = 1212    val task→ ⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩] = FutureTask(Callable { scoreBatch(seed) })13    val worker→ Thread[#19,Thread-0,5,main] = Thread(task⟨FutureTask B⟩[Not completed, task = ⟨MainKt lambda A⟩])1415    workerThread[#19,Thread-0,5,main].start()16    worker.join()
  2. fun scoreBatch(seed: Int): Int

    4fun scoreBatch(seed12: Int): Int {5    val base = seed + 4
  3. base ← 16, bonus ← 8

    4fun scoreBatch(seed: Int): Int {5    val base→ 16 = seed12 + 46    val bonus→ 8 = base16 / 27    return base16 + bonus88}910fun main() {11    val seed = 1212    val task = FutureTask(Callable { scoreBatch(seed) })13    val worker = Thread(task)1415    worker.start()16    workerThread[#19,Thread-0,5,main].join()
  4. worker ← Thread[#19,Thread-0,5,], score ← 24

    15    worker.start()16    worker→ Thread[#19,Thread-0,5,].join()1718    val score→ 24 = task⟨FutureTask B⟩[Completed normally].get()19    println("seed=$seed12")20    println("score=$score24")21}
    outputseed=12
    score=24