未解决的参考线程

时间:2018-05-31 09:47:18

标签: multithreading concurrency kotlin standard-library

我正在尝试使用import matplotlib.pyplot as plt plt.plot(y,'o');plt.plot(res.fun+y);plt.plot(res.fun) 在kotlin中使用kotlin.concurrency.thread启动一个新线程但是我一直在:

 Unresolved reference: thread

我以为这是在标准库中?

实际代码:

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

1 个答案:

答案 0 :(得分:0)

正确的导入语句是:

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块即可。