What is the correct way to create a thread-safe, infinite circular iterator in scala? It seems that the following is not thread safe (iterating simulataneously from multiple threads on iterator occasionally throws exceptions):
val map = Map(1->"one", 2->"two")
val iterator = Iterator.continually(map).flatten
How would you correct this to make it thread-safe?
I've ran into a same question but I think we can do this to be safe as implementation independent as discussed here.
iterator.synchronized(
iterator.next()
)
Just wrap to the Provider. smth like this:
class Iterator2ProviderAdapter[A](iterator: Iterator[A]) {
def get: A = synchronized(iterator.next())
}
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