Skip to content

Instantly share code, notes, and snippets.

@tmclnk
Created May 26, 2015 13:20
Show Gist options
  • Save tmclnk/3545826cba42b19b8112 to your computer and use it in GitHub Desktop.
Save tmclnk/3545826cba42b19b8112 to your computer and use it in GitHub Desktop.
GPARS Data Flow
import static groovyx.gpars.dataflow.Dataflow.*
import groovyx.gpars.dataflow.*
// The Dataflow Variables have a pretty straightforward semantics.
// When a task needs to read a value from DataflowVariable
// (through the val property), it will block until the value has been
// set by another task or thread (using the '<<' operator).
// Each DataflowVariable can be set only once in its lifetime.
// Notice that you don't have to bother with ordering and synchronizing
// the tasks or threads and their access to shared variables. The values
// are magically transferred among tasks at the right time without your intervention.
// The data flow seamlessly among tasks / threads without your intervention or care.
final def x = new DataflowVariable()
final def y = new DataflowVariable()
final def z = new DataflowVariable()
task {
z << x.val + y.val
}
task {
x << 10
}
task {
y << 5
}
println "Result: ${z.val}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment