仓颉并发编程示例
spawn 的使用
主线程和新线程同时尝试打印一些文本。
代码如下:
main(): Int64 {
spawn { =>
for (i in 0..10) {
println("New thread, number = ${i}")
sleep(100 * Duration.millisecond) /* 睡眠 100 毫秒 */
}
}
for (i in 0..5) {
println("Main thread, number = ${i}")
sleep(100 * Duration.millisecond) /* 睡眠 100 毫秒 */
}
return 0
}
运行结果如下:
Main thread, number = 0
New thread, number = 0
Main thread, number = 1
New thread, number = 1
Main thread, number = 2
New thread, number = 2
Main thread, number = 3
New thread, number = 3
Main thread, number = 4
New thread, number = 4
New thread, number = 5
注意:
上述打印信息仅做参考,实际结果受运行时序影响,呈现随机性。
由于主线程不会等待新线程执行结束,因此程序退出时新线程并未执行结束。
Future 的 get 的使用
主线程等待创建线程执行完再执行。
代码如下:
main(): Int64 {
let fut: Future<Unit> = spawn { =>
for (i in 0..10) {
println("New thread, number = ${i}")
sleep(100 * Duration.millisecond) /* 睡眠 100 毫秒 */
}
}
fut.get() /* 等待线程完成 */
for (i in 0..5) {
println("Main thread, number = ${i}")
sleep(100 * Duration.millisecond) /* 睡眠 100 毫秒 */
}
return 0
}
运行结果如下:
New thread, number = 0
New thread, number = 1
New thread, number = 2
New thread, number = 3
New thread, number = 4
New thread, number = 5
New thread, number = 6
New thread, number = 7
New thread, number = 8
New thread, number = 9
Main thread, number = 0
Main thread, number = 1
Main thread, number = 2
Main thread, number = 3
Main thread, number = 4
取消仓颉线程
子线程接收主线程发送的取消请求。
main(): Unit {
let future = spawn { // Create a new thread
while (true) {
if (Thread.currentThread.hasPendingCancellation) {
return 0
}
}
return 1
}
//...
future.cancel() // Send a termination request
let res = future.get() // Wait for thread termination
println(res) // 0
}
运行结果:
0