gpt4 book ai didi

multithreading - Scala并行集合: Idiomatic way of having thread-local-variables for worker threads

转载 作者:行者123 更新时间:2023-12-03 13:04:55 25 4
gpt4 key购买 nike

下面的进度功能是我的 worker 功能。我需要让它访问一些创建/获取成本很高的类。库中是否有用于线程局部变量的标准机器?还是我必须自己编写一个对象池管理器?

object Start extends App {
def progress {
val current = counter.getAndIncrement
if(current % 100 == 0) {
val perc = current.toFloat * 100 / totalPosts
print(f"\r$perc%4.2f%%")
}
}

val lexicon = new Global()

def processTopic(forumId: Int, topicId: Int) {
val(topic, posts) = ProcessingQueries.getTopicAndPosts(forumId,topicId)

progress
}



val (fid, tl) = ProcessingQueries.getAllTopics("hon")
val totalPosts = tl.size
val counter = new AtomicInteger(0)
val par = tl.par

par.foreach { topic_id =>
processTopic(fid,topic_id)
}
}

最佳答案

替换了先前的答案。这样做的技巧很好而且很整洁

object MyAnnotator extends ThreadLocal[StanfordCoreNLP] {
val props = new Properties()
props.put("annotators", "tokenize,ssplit,pos,lemma,parse")
props.put("ssplit.newlineIsSentenceBreak", "two")
props.put("parse.maxlen", "40")

override def initialValue = new StanfordCoreNLP(props)
}

关于multithreading - Scala并行集合: Idiomatic way of having thread-local-variables for worker threads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20730694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com