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.
java.lang.Object
and implement such antipatterns easily.actor
from coroutines is deprecated, so this is also not good option.