Reputation: 61
I'm trying to figure out if there is a 'groovier' way to iterate thread safe in Groovy than the usual way in Java iterating a Collections.synchronizedList
inside a synchronized block, but I've found no reference to this issue (http://groovy.codehaus.org/Looping).
Are each
and eachWithIndex
thread-safe? If not, is there an easy way to iterate thread-safe already provided by Groovy? Or should I use the old Java way?
Thanks in advance.
Upvotes: 3
Views: 1452
Reputation: 12207
Groovy's each uses iterators underneath (see the each method in DefaultGroovyMethods). As such, no, they would not be thread-safe in and of themselves. But you could obviously still use them within a synchronized block. You might also be interested in the @Synchronized AST transformation.
Upvotes: 1