yoyoooyoy
yoyoooyoy

Reputation: 61

Thread safe iteration in Groovy

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

Answers (1)

Keegan
Keegan

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

Related Questions