Using threads and AtomicInteger
From Groovy you can use all of the normal concurrency facilities in Java and combine them with threads and closures as necessary. E.g. a (slightly modified) atomic counter from a Groovy example here:
The output will be something like this:
Fibonacci with Executors
A Groovy script to naively calculate the Fibonacci series inspired by the example here.
Note: a version using memoizing will be much more efficient but this illustrates using Java's built-in concurrency primitives from Groovy.
Which produces this:
If you want to convince yourself that some threads are actually being created, replace the last
each line with:
which will add something like the following to your output:
Fibonacci with Functional Java
If you wish to make use of the functionaljava (tested with 2.1.3) library (see the link to the original example), you could use a Groovy program such as this:
Catching Exceptions with an Exception Handler
When catching Exceptions with an exception handler in Groovy Scripts, there is potential for interaction with Groovy's runtime which catches exceptions and filters stacktraces. The best way to avoid this interaction is to create your own thread and call
setDefaultUncaughtExceptionHandler directly on that thread instance as per below: