我想我从医生那里弄错了。
我有两个演员,XMLActor和HttpActor。XMLActor读取xmlFiles,然后向HTTPActor发送一条消息进行处理。XMLActor将比HttpActor更快完成。
我的主类对两个参与者都调用join。我原以为主线程只有在两个角色都完成后才会终止。但是,实际发生的情况是,一旦XMLActor处理完所有消息,系统就会终止,并且许多消息不会被HttpActor处理。
我可以使用一些闩锁甚至AtomicInteger来等待所有消息被使用,但我想知道是否有更优雅的方法。
final HttpActor httpActor = new HttpActor().start()
final XMLActor xmlActor = new XMLActor(httpActor:httpActor).start()
Actors.actor {
file.eachLine { line ->
def chunks = line.split(",")
def id = chunks[0].replaceAll("\\\"","").trim()
def name = chunks[1].replaceAll("\\\"","").trim()
xmlActor << new FileToRead(basePath:args[1],id:id,name:name, fileCounter:counter)
}
}
[httpActor, xmlActor]*.join()
//inside xmlActor
countries.each { country ->
httpActor << new AlbumPriceMessage(id:message.id, country:country)
}发布于 2013-02-22 12:55:43
join()方法肯定会等待两个参与者都完成。我不明白你是怎么阻止这两个演员的,所以我真的不能对此发表评论。你会发送这种有害的信息吗?或者在actors上调用stop()?
例如,您的案例的以下模拟将正确停止:
import groovyx.gpars.actor.*;
def httpActor = Actors.staticMessageHandler {
println "Http actor processing " + it
}
def xmlActor = Actors.staticMessageHandler {
println "XML Actor processing " + it
httpActor << it
}
xmlActor.metaClass.afterStop = {
httpActor.stop()
}
100.times {
xmlActor << "File$it"
}
xmlActor.stop()
[xmlActor, httpActor]*.join()
println "done"https://stackoverflow.com/questions/15013751
复制相似问题