Kotlin - 等待函数
Kotlin - Wait function
kotlin有wait函数吗?(我说的不是Timer Schedule,而是暂停执行)。我了解到您可以使用 Thread.sleep()
。但是,它对我不起作用,因为找不到该功能。
线程睡眠总是需要等待多长时间:
https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)
public static void sleep(long millis)
throws InterruptedException
例如
Thread.sleep(1_000) // wait for 1 second
如果你想等待其他线程唤醒你,也许 `Object#wait()' 会更好
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()
public final void wait()
throws InterruptedException
然后另一个线程必须调用 yourObject#notifyAll()
例如
Thread1 和 Thread2 共享一个 Object o = new Object()
Thread1: o.wait() // sleeps until interrupted or notified
Thread2: o.notifyAll() // wake up ALL waiting Threads of object o
请试试这个,它适用于 Android:
Handler(Looper.getMainLooper()).postDelayed(
{
// This method will be executed once the timer is over
},
1000 // value in milliseconds
)
自从 Kotlin 1.1 版中提供了新的协程功能后,您可以使用具有以下签名的 非阻塞 delay
函数:
suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS)
在 Kotlin 1.2 中它仍然位于 kotlinx.coroutines.experimental
包中。 here.
描述了协程的实验状态
更新: Kotlin 1.3 已发布,协程已移至 kotlinx.coroutines
包,它们不再是实验性功能。
UPD 2: 要在不可挂起的方法中使用此方法,可以像任何其他可挂起的方法一样通过 runBlocking {}
:
runBlocking {
delay(2, TimeUnit.SECONDS)
}
您可以使用标准 JDK 的内容。
另一个回复表明 Thread.sleep(millis: Long)
。我个人更喜欢 TimeUnit class(自 Java 1.5 起),它提供了更全面的语法。
TimeUnit.SECONDS.sleep(1L)
TimeUnit.MICROSECONDS.sleep(1000_000L)
他们在幕后使用 Thread.sleep
,他们也可以抛出 InterruptedException。不幸的是,正如@AndroidDev 在评论中指出的那样,它们不与 Kotlin 协程合作(自 Kotlin 1.1 起)。所以在这种情况下我们应该更喜欢 delay
函数。
public suspend fun delay(timeMillis: Long): Unit
public suspend fun delay(duration: Duration): Unit
您可以使用 Kotlin 协程轻松实现此目的
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
CoroutineScope(Dispatchers.IO).launch {
delay(TimeUnit.SECONDS.toMillis(3))
withContext(Dispatchers.Main) {
Log.i("TAG", "this will be called after 3 seconds")
finish()
}
}
Log.i("TAG", "this will be called immediately")
}
}
build.gradle(应用程序)
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
}
您可以使用
SystemClock.sleep(1000)
你可以用来等待或休眠 2 秒
TimeUnit.SECONDS.sleep(2L)
TimeUnit.SECONDS.sleep((seconds you want to sleep) + L)
kotlin有wait函数吗?(我说的不是Timer Schedule,而是暂停执行)。我了解到您可以使用 Thread.sleep()
。但是,它对我不起作用,因为找不到该功能。
线程睡眠总是需要等待多长时间: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)
public static void sleep(long millis)
throws InterruptedException
例如
Thread.sleep(1_000) // wait for 1 second
如果你想等待其他线程唤醒你,也许 `Object#wait()' 会更好
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()
public final void wait()
throws InterruptedException
然后另一个线程必须调用 yourObject#notifyAll()
例如
Thread1 和 Thread2 共享一个 Object o = new Object()
Thread1: o.wait() // sleeps until interrupted or notified
Thread2: o.notifyAll() // wake up ALL waiting Threads of object o
请试试这个,它适用于 Android:
Handler(Looper.getMainLooper()).postDelayed(
{
// This method will be executed once the timer is over
},
1000 // value in milliseconds
)
自从 Kotlin 1.1 版中提供了新的协程功能后,您可以使用具有以下签名的 非阻塞 delay
函数:
suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS)
在 Kotlin 1.2 中它仍然位于 kotlinx.coroutines.experimental
包中。 here.
更新: Kotlin 1.3 已发布,协程已移至 kotlinx.coroutines
包,它们不再是实验性功能。
UPD 2: 要在不可挂起的方法中使用此方法,可以像任何其他可挂起的方法一样通过 runBlocking {}
:
runBlocking {
delay(2, TimeUnit.SECONDS)
}
您可以使用标准 JDK 的内容。
另一个回复表明 Thread.sleep(millis: Long)
。我个人更喜欢 TimeUnit class(自 Java 1.5 起),它提供了更全面的语法。
TimeUnit.SECONDS.sleep(1L)
TimeUnit.MICROSECONDS.sleep(1000_000L)
他们在幕后使用 Thread.sleep
,他们也可以抛出 InterruptedException。不幸的是,正如@AndroidDev 在评论中指出的那样,它们不与 Kotlin 协程合作(自 Kotlin 1.1 起)。所以在这种情况下我们应该更喜欢 delay
函数。
public suspend fun delay(timeMillis: Long): Unit
public suspend fun delay(duration: Duration): Unit
您可以使用 Kotlin 协程轻松实现此目的
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
CoroutineScope(Dispatchers.IO).launch {
delay(TimeUnit.SECONDS.toMillis(3))
withContext(Dispatchers.Main) {
Log.i("TAG", "this will be called after 3 seconds")
finish()
}
}
Log.i("TAG", "this will be called immediately")
}
}
build.gradle(应用程序)
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
}
您可以使用
SystemClock.sleep(1000)
你可以用来等待或休眠 2 秒
TimeUnit.SECONDS.sleep(2L)
TimeUnit.SECONDS.sleep((seconds you want to sleep) + L)