我有这段代码:
class GitDownloader extends DefaultActor {
private String dir
@Override
protected void act() {
loop {
react {
repos -> if (repos instanceof String[])
println "Total Repos: ${repos.size()}"
repos.each {
repo ->
"git clone ssh://git@bar-src.foo.com/${repo} ${dir}/${repo.split("/").last()}".execute()
}
}
}
}
}
我这样开始:
def gitDownloader = new GitDownloader(dir: sourceCodeFolder)
gitDownloader.start()
gitDownloader << repositories
问题是任务以随机顺序执行。
它应该像repos.size()那样多次运行git clone
命令,但事实并非如此。它每次都会随机运行一次。
each
循环应该作为单个任务在后台执行,似乎git clone
命令是并行执行的。
答案 0 :(得分:2)
如果您要发送一个列表,它将是一个收到的列表......
以下代码:
import groovyx.gpars.actor.*
class GitDownloader extends DefaultActor {
private String dir
@Override
protected void act() {
loop {
react { repos ->
println "Got an ${repos.getClass()} of repos"
repos.each {
repo ->
println "doing $repo for dir $dir"
}
}
}
}
}
def g = new GitDownloader(dir:'woo')
g.start()
g << ['a', 'b']
打印:
Got an class java.util.ArrayList of repos
doing a for dir woo
doing b for dir woo
这似乎按预期工作......
或者将其更改为一次执行一项(添加睡眠以模拟正在完成的某项工作)
import groovyx.gpars.actor.*
class GitDownloader extends DefaultActor {
private String dir
@Override
protected void act() {
loop {
react { repo ->
println "Got an ${repo.getClass()} of repos"
println "doing $repo for dir $dir"
Thread.sleep(1000)
}
}
}
}
def g = new GitDownloader(dir:'woo')
g.start()
g << 'a' << 'b'
g
同样有效,并打印:
Got an class java.lang.String of repos
doing a for dir woo
Got an class java.lang.String of repos
doing b for dir woo
从您的代码中,我看不到问题(除String[]
之外的事情)