Post

(Kotlin) thread

usage
1
2
3
4
5
6
7
8
9
thread {
// do something
/\*
runOnUiThread {
// update UI
}
\*/
}

definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/\*\*
\* Creates a thread that runs the specified [block] of code.
\*
\* @param start if `true`, the thread is immediately started.
\* @param isDaemon if `true`, the thread is created as a daemon thread. The Java Virtual Machine exits when
\* the only threads running are all daemon threads.
\* @param contextClassLoader the class loader to use for loading classes and resources in this thread.
\* @param name the name of the thread.
\* @param priority the priority of the thread.
\*/
public fun thread(start: Boolean = true,
isDaemon: Boolean = false,
contextClassLoader: ClassLoader? = null,
name: String? = null,
priority: Int = -1,
block: () -> Unit): Thread {
val thread = object : Thread() {
public override fun run() {
block()
}
}
if (isDaemon)
thread.isDaemon = true
if (priority > 0)
thread.priority = priority
if (name != null)
thread.name = name
if (contextClassLoader != null)
thread.contextClassLoader = contextClassLoader
if (start)
thread.start()
return thread
}

This post is licensed under CC BY 4.0 by the author.