Correctly implementing wait and notify in Kotlin

2020-03-08 08:47发布

问题:

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

wait()/notify()

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()
    }
}

EDIT:

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

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

回答1:

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

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.

For example, if your Java code was something like:

private Object lock = new Object();

...

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

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()
    ...
}

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).

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



回答2:

A BlockingQueue can be a suitable high-level concurrency utility for your use case, but applying it requires knowing and modifying your code structure.

The idea is, fetchData() should .take() an item from the queue, and if the queue is empty, that will block the execution until an item appears, which eliminates the .wait() in your code. The producer of the data should .put(t) the data into the queue.


If you really need to use wait and notify, e.g. for implementing a concurrency utility at low-level, you can cast a Kotlin object to java.lang.Object and call these functions afterwards, as said in the language reference. Or, written as extension functions:

@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
private fun Any.wait() = (this as java.lang.Object).wait()


回答3:

You could use the kotlin Mutex to suspend the coroutine instead of blocking a thread:

val mutex = Mutex(true)

suspend fun runFetchLoop() {
    while (true) {
        val data = fetchData()
        processData(data)
        if (data.isEmpty()) {
            mutex.lock()
        }
    }
}

fun notify() = mutex.unlock()

Here is a running example: https://pl.kotl.in/Q6V0X8DvL .

However I would prefer a cold Flow or a hot Channel to solve this problem. Here is a good article about cold flows and hot channels of the great Roman Elizarov



标签: kotlin