gpt4 book ai didi

multithreading - 未解析的引用线程

转载 作者:行者123 更新时间:2023-12-02 13:38:29 25 4
gpt4 key购买 nike

我正在尝试使用kotlin.concurrency.thread在kotlin中为android启动一个新线程,但是我不断得到:

 Unresolved reference: thread

我以为这是在标准库中?

实际代码:
fun identify(userId: Integer) {
thread() {
CustomExceptionHandler(context)
DoStuffClass.doStuff(context, userId)
}
}

最佳答案

正确的导入语句是:

import kotlin.concurrent

示例代码应为:
fun identify(userId: Integer) {
thread {
CustomExceptionHandler(context)
DoStuffClass.doStuff(context, userId)
}
}

因为 thread函数定义为:
/**
* 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 {
...
}

here所述, thread函数使用lambda作为最后一个参数,然后,按照Kotlin语法,单个参数函数不需要括号,只需要lambda块。

关于multithreading - 未解析的引用线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50621361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com