Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note: GPars comes bundled with Groovy 1.8.x distributions so this step is only requited if you are using an older version of Groovyshould normally be not required.

We'll use Groovy's Grape functionaity to grab all the requited dependencies for us. You may check ot the GPars Integration page for alternative ways to integrate GPars with your project.

...

Code Block
@Grab(group='org.codehaus.gpars', module='gpars', version='1.1.0.12')

Step 3 - Experiment with parallel collection processing

...

Code Block
@Grab(group='org.codehaus.gpars', module='gpars', version='1.1.0.12')
import groovyx.gpars.GParsPool

GParsPool.withPool {
    def animals = ['dog', 'ant', 'cat', 'whale']
    println(animals.anyParallel {it ==~ /ant/} ? 'Found an ant' : 'No ants found')
    println(animals.everyParallel {it.contains('a')} ? 'All animals contain a' : 'Some animals can live without an a')
}

...

To find out more about parallel collection processing, visit the Parallel Colections Collections section of the User Guide.

Step 4 - Actors

...

Code Block
@Grab(group='org.codehaus.gpars', module='gpars', version='1.1.0.12')
import groovyx.gpars.actor.DynamicDispatchActor
import org.codehaus.groovy.runtime.NullObject

final class MyActor extends DynamicDispatchActor {
    private int counter = 0

    void onMessage(String message) {
        counter += message.size()
        println 'Received string'
    }

    void onMessage(Integer message) {
        counter += message
        println 'Received integer'
    }

    void onMessage(Object message) {
        counter += 1
        println 'Received object'
    }

    void onMessage(NullObject message) {
        println 'Received a null object. Sending back the current counter value.'
        reply counter
    }
}

final def actor = new MyActor()
actor.start()
actor.send 1
actor << 2
actor 20
actor 'Hello'
println actor.sendAndWait(null)

...