Kotlin note thread (11)
Kotlin note data type (1) Kotlin note string (2) Kotlin note operator (3) Kotlin note function (4) Kotlin note object-oriented (5) Kotlin note inheritance, abstract class, interface (6) Kotlin note high-order function (7) Kotlin Notes Generics (8) Kotlin Notes Data Containers (9) Kotlin Notes Functional Programming API (10)
Article directory
1. Thread creation
fun main(args: Array<String>) {
// public fun thread(
// start: Boolean = true, //创建完成是否马上启动
// isDaemon: Boolean = false, // 是否为守护线程 守护线程是
//一种在后台长期运行线程,守护线程主要提供一些后台服务,它的生命周期与Java虚拟机
//一样长
// contextClassLoader: ClassLoader? = null, // 类加载器,用来加载一些资源等;
// name: String? = null, //指 定线程名,如果不指定线程名
// priority: Int = -1, //设置线程优先 级
// block: () -> Unit //是线程要执行的核心代码。
// ): Thread
thread {
run()
}
thread (name = "zyb"){
run()
}
}
fun run(){
for(i in 0..9){
println("第${i}次执行 - ${currentThread().name}")
// 随机生成休眠时间
val sleepTime = (1000 * random()).toLong()
// 线程休眠
sleep(sleepTime)
}
println("执行完成! " + currentThread().name)
}
2. Thread state
-
New state
The new state (New) is to create a thread object by instantiating Thread, which is just an empty thread
object -
After the start() function of the new thread is created in the ready state
, it enters the ready state (Runnable). At this time
, the thread has not actually started to execute the thread body, it must wait for the scheduling of the CPU -
Running state
The thread in the scheduling ready state of the CPU, the thread enters the running state (Running), the thread in the running state
monopolizes the CPU, and the execution of the thread body is completed -
In the blocked state,
for some reason, the thread in the running state will enter the non-running state, that is, the blocked state (Blocked). The
Java virtual machine system of the thread in the blocked state cannot execute the thread, even if the CPU is idle, it cannot execute the
thread. The following reasons will cause the thread to enter the blocked state:
- The current thread calls the sleep function and enters the sleep state.
- The join function is called by other threads, waiting for other threads to end.
- Issue an I/O request and wait for the I/O operation to complete.
- The current thread calls the wait function.
- Dead state
After the thread executes the thread body, it will enter the dead state (Dead). The thread entering the dead state may be due to the
completion of normal execution, or it may enter due to an exception.
3. Thread management
1、join
Calling the join method of the child thread by the current thread will block the current thread, and the child thread will be executed first
fun main(args: Array<String>) {
var thread1=thread {
run()
}
thread1.join()
var thread2=thread (name = "zyb"){
run()
}
}
fun run( ){
for(i in 0..9){
println("第${i}次执行 - ${currentThread().name}")
// 随机生成休眠时间
val sleepTime = (1000 * random()).toLong()
// 线程休眠
sleep(sleepTime)
}
println("执行完成! " + currentThread().name)
}
第0次执行 - Thread-0
第1次执行 - Thread-0
第2次执行 - Thread-0
第3次执行 - Thread-0
第4次执行 - Thread-0
第5次执行 - Thread-0
第6次执行 - Thread-0
第7次执行 - Thread-0
第8次执行 - Thread-0
第9次执行 - Thread-0
执行完成! Thread-0
第0次执行 - zyb
第1次执行 - zyb
第2次执行 - zyb
第3次执行 - zyb
第4次执行 - zyb
第5次执行 - zyb
第6次执行 - zyb
第7次执行 - zyb
第8次执行 - zyb
第9次执行 - zyb
执行完成! zyb