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:
Calculating Fibonacci sequence in parallel... n=8 => 21 n=9 => 34 n=10 => 55 n=11 => 89 n=12 => 144 n=13 => 233 n=14 => 377 n=15 => 610 n=16 => 987
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:
[Thread[Finalizer,8,system], Thread[Java2D Disposer,10,system], Thread[Image Fetcher 0,8,main], Thread[Reference Handler,10,system], Thread[AWT-Shutdown,5,main], Thread[pool-1-thread-4,5,main], Thread[Thread-3,6,main], Thread[pool-1-thread-3,5,main], Thread[Attach Listener,5,system], Thread[Thread-2,6,main], Thread[Signal Dispatcher,9,system], Thread[TimerQueue,5,system], Thread[Image Fetcher 1,8,main], Thread[pool-1-thread-1,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-6,5,main], Thread[AWT-EventQueue-0,6,main], Thread[AWT-Windows,6,main], Thread[DestroyJavaVM,5,main], Thread[pool-1-thread-2,5,main]]
Labels