Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 705
yoyoooyoy Avatar asked Feb 13 '26 15:02

yoyoooyoy


1 Answers

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.

like image 95
Keegan Avatar answered Feb 15 '26 10:02

Keegan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!