All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.parallel.ThreadGuard.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.parallel

// RAII Thread Wrapper
class Async(
    val task: () -> T
) : AutoCloseable {
    private var result: T? = null

    val thread = Thread {
        val result = task()
        this.result = result
    }

    override fun close() {
        thread.join()
    }

    init {
        thread.start()
    }

    fun join(): T {
        thread.join()
        return result!!
    }
}

typealias ThreadGuard = Async




© 2015 - 2024 Weber Informatics LLC | Privacy Policy