Hi, I have a class which contains a map of accumulator values, like this:
private val accumulators = mutableMapOf(
"foo" to 0,
"bar" to 0,
)
Then later on in an init block, I'm doing a heavy IO operation to count all the values. I'm walking a large file tree:
init {
MyScope(Dispatchers.IO).launch {
// Walk file tree and accumulate values in map
}
}
finally I have them declared in the class as delegates:
val foo by accumulators
val bar by accumulators
How can I ensure that the
init
block runs before
foo
and
bar
are accessed? I'm using Swing, and when the UI loads, the user can click to display information. This operation inside
init
can take a few seconds, and it's possible the user requests the information during accumulation. Is there some sort of locking mechanism I can use so that when the user requests the info, it will wait in the backgorund and then show on the UI?