数据流变量
import static groovyx.gpars.dataflow.Dataflow.taskfinal def x = new DataflowVariable() final def y = new DataflowVariable() final def z = new DataflowVariable()
task { z << x.val + y.val println "Result: ${z.val}" }
task { x << 10 }
task { y << 5 }
数据流
import static groovyx.gpars.dataflow.Dataflow.taskfinal def df = new Dataflows()
task { df.z = df.x + df.y println "Result: ${df.z}" }
task { df.x = 10 }
task { df.y = 5 }
数据流队列
import static groovyx.gpars.dataflow.Dataflow.taskdef words = ['Groovy', 'fantastic', 'concurrency', 'fun', 'enjoy', 'safe', 'GPars', 'data', 'flow'] final def buffer = new DataflowQueue()
task { for (word in words) { buffer << word.toUpperCase() //add to the buffer } }
task { while(true) println buffer.val //read from the buffer in a loop }
绑定处理程序
def a = new DataflowVariable() a >> {println "The variable has just been bound to $it"} a.whenBound {println "Just to confirm that the variable has been really set to $it"} ...
数据流运算符
operator(inputs: [a, b, c], outputs: [d]) {x, y, z ->
…
bindOutput 0, x + y + z
}