2018-12-01 00:02:18 +01:00
|
|
|
package jp.juggler.util
|
|
|
|
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
// threading
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
val isMainThread: Boolean get() = Looper.getMainLooper().thread === Thread.currentThread()
|
2018-12-01 00:02:18 +01:00
|
|
|
|
2020-12-17 04:17:48 +01:00
|
|
|
// メインスレッドから呼び出された場合、それがsynchronizedの中だと GlobalScope.launch を使うのは良くない
|
2021-06-20 15:12:25 +02:00
|
|
|
fun runOnMainLooper(proc: () -> Unit) {
|
|
|
|
val looper = Looper.getMainLooper()
|
|
|
|
if (looper.thread === Thread.currentThread()) {
|
|
|
|
proc()
|
|
|
|
} else {
|
|
|
|
Handler(looper).post { proc() }
|
|
|
|
}
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
fun runOnMainLooperDelayed(delayMs: Long, proc: () -> Unit) {
|
|
|
|
Handler(Looper.getMainLooper()).postDelayed({ proc() }, delayMs)
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|