44 Accessing Thread
HardNewly created threads in Cangjie can terminate early if the thread that created them terminates. However, you can use the return value of a thread to wait until execution is complete.
The return value of a thread is Future<T>. You can think of it as a box that promises this value will be computed; you just need to wait.
Normal function call.
The return value of a thread is of type Future<Int64>.
Here we use one of the functions on Future, get(), which waits for a thread to finish execution and produce a value. This way, the main thread will not finish before the newly created ones.
accesing_thread.cj
func sum(nums: Array<Int64>): Int64 {
var total = 0
for (i in nums) {
total += i
}
return total
}
main() {
var sum_even = sum(Array<Int64>(10, {i => 2 * i}))
var sum_odd: Future<Int64> = spawn {
sum(Array<Int64>(10, {i => 2 * i + 1}))
}
println(sum_even)
println(sum_odd.get())
}
// Output:
// 90
// 100