Skip to content

Commit 50930ae

Browse files
authored
update: warning suppression on Any (#5216)
1 parent 12aae40 commit 50930ae

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

docs/topics/jvm/java-interop.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,46 @@ so to make other members of `java.lang.Object` available, Kotlin uses [extension
687687
### wait()/notify()
688688

689689
Methods `wait()` and `notify()` are not available on references of type `Any`. Their usage is generally discouraged in
690-
favor of `java.util.concurrent`. If you really need to call these methods, you can cast to `java.lang.Object`:
690+
favor of `java.util.concurrent`.
691+
692+
If you have to call these methods, cast to `java.lang.Object` and suppress the warning:
691693

692694
```kotlin
693-
(foo as java.lang.Object).wait()
695+
import java.util.LinkedList
696+
697+
class SimpleBlockingQueue<T>(private val capacity: Int) {
698+
private val queue = LinkedList<T>()
699+
700+
// java.lang.Object is used specifically to access wait() and notify()
701+
// In Kotlin, the standard 'Any' type does not expose these methods.
702+
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
703+
private val lock = Object()
704+
705+
fun put(item: T) {
706+
synchronized(lock) {
707+
while (queue.size >= capacity) {
708+
lock.wait()
709+
}
710+
queue.add(item)
711+
println("Produced: $item")
712+
713+
lock.notifyAll()
714+
}
715+
}
716+
717+
fun take(): T {
718+
synchronized(lock) {
719+
while (queue.isEmpty()) {
720+
lock.wait()
721+
}
722+
val item = queue.removeFirst()
723+
println("Consumed: $item")
724+
725+
lock.notifyAll()
726+
return item
727+
}
728+
}
729+
}
694730
```
695731

696732
### getClass()

0 commit comments

Comments
 (0)