Added Thread to retrieve information about the current thread

This commit is contained in:
dankito 2020-07-14 17:05:59 +02:00
parent 52d3b49baa
commit 98056a520a
3 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package net.dankito.utils.multiplatform
expect class Thread() {
companion object {
val current: Thread
}
val threadName: String
}

View File

@ -0,0 +1,33 @@
package net.dankito.utils.multiplatform
import platform.Foundation.NSOperationQueue
import platform.Foundation.NSThread
actual class Thread(private val thread: NSThread) {
actual companion object {
actual val current: Thread
get() = Thread(NSThread.currentThread)
}
actual constructor() : this(NSThread())
actual val threadName: String
get() {
thread.name?.let { name ->
if (name.isNotBlank()) {
return name
}
}
return thread.description
?: NSOperationQueue.currentQueue?.underlyingQueue?.description
?: "Could not retrieve thread's name"
}
}

View File

@ -0,0 +1,20 @@
package net.dankito.utils.multiplatform
actual class Thread(private val thread: java.lang.Thread) : java.lang.Thread() {
actual companion object {
actual val current: Thread
get() = Thread(currentThread())
}
actual constructor() : this(java.lang.Thread())
actual val threadName: String
get() = thread.name
}