I have created a set using Collections.synchronizedSet<T>(mutableSetOf<T>())
.
SynchronizedSet
has its own implementation of forEach
(synchronized) that differs from the one provided by Iterable.forEach
(not synchronized), however Kotlin's Iterable.forEach
is annotated with @HidesMembers
, so it gets called instead of the synchronized one.
How do I get back the synchronized version of forEach
?
The forEach
you are looking for is a method on java.lang.Iterable
interface so casting will work.
A (quite ugly) example:
import java.util.Collections
fun main() {
val col = Collections.synchronizedCollection(mutableSetOf(1,2,3))
(col as java.lang.Iterable<*>).forEach { println(it) }
}
The IDEA debugger hits the expected code in SynchronizedCollection.forEach
:
public void forEach(Consumer<? super E> consumer) {
synchronized (mutex) {c.forEach(consumer);}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With