在Kotlin中正确执行等待和通知 [英] Correctly implementing wait and notify in Kotlin

查看:736
本文介绍了在Kotlin中正确执行等待和通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据本文档,不建议在Kotlin中使用waitnotify: https://kotlinlang.org/docs/reference/java-interop.html

According to this document, using wait and notify is discouraged in Kotlin: https://kotlinlang.org/docs/reference/java-interop.html

wait()/notify()

wait()/notify()

有效的Java项目69建议使用并发实用程序,而不是wait()和notify().因此,这些方法在类型为Any的引用上不可用.

Effective Java Item 69 kindly suggests to prefer concurrency utilities to wait() and notify(). Thus, these methods are not available on references of type Any.

但是该文档没有提出任何正确的处理方法.

However the document does not propose any correct way of doing it.

基本上,我想实现一个服务,该服务将读取输入数据并进行处理.如果没有输入数据,它将暂停自身直到有人通知有新的输入数据.像

Basically, I would like to implement a service, which would read the input data and process them. If there were no input data, it would suspend itself until someone notifies that there are new input data. Something like

while (true) {
    val data = fetchData()
    processData(data)
    if (data.isEmpty()) {
        wait()
    }
}

我不想使用这些不推荐的方法(反模式),我真的想了解如何正确地做到这一点.

I don't want to use these not recommended methods (antipatterns), I really want to find out how to do this properly.

在我的情况下fetchData从数据库中读取数据,因此在我的情况下无法使用队列.

In my case fetchData reads data from the database, so queues in my case cannot be used.

推荐答案

通常,应尽可能使用更高级别的并发实用程序.

In general you should use higher-level concurrency utilities when possible.

但是,如果在您的情况下没有更高层次的构造起作用,则直接 替换是使用 ReentrantLock 和一个单身的 Condition 在那个锁上.

However, if none of the higher-level constructs work in your case, the direct replacement is to use a ReentrantLock and a single Condition on that lock.

例如,如果您的Java代码类似于:

For example, if your Java code was something like:

private Object lock = new Object();

...

synchronized(lock) {
    ...
    lock.wait();
    ...
    lock.notify();
    ...
    lock.notifyAll();
    ...
}

您可以将其更改为以下Kotlin:

You can change it to the following Kotlin:

private val lock = ReentrantLock()
private val condition = lock.newCondition()

lock.withLock {           // like synchronized(lock)
    ...
    condition.await()     // like wait()
    ...
    condition.signal()    // like notify()
    ...
    condition.signalAll() // like notifyAll()
    ...
}

虽然这有点冗长,但条件确实可以提供一些额外的信息 灵活性,因为您可以在一个锁上拥有多个条件 还有其他类型的锁(尤其是ReentrantReadWriteLock.ReadLockReentrantReadWriteLock.WriteLock).

While this is slightly more verbose, conditions do provide some extra flexibility, as you can have multiple conditions on a single lock, and there are also other kinds of locks (notably ReentrantReadWriteLock.ReadLock and ReentrantReadWriteLock.WriteLock).

请注意,withLock是Kotlin提供的扩展功能,可以在调用提供的Lambda之前/之后调用Lock.lock()/Lock.unlock().

Note that withLock is a Kotlin-provided extension function that takes care of calling Lock.lock()/Lock.unlock() before/after invoking the supplied lambda.

这篇关于在Kotlin中正确执行等待和通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆