2

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

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)
    }
}
4

1 回答 1

1

替换了之前的答案。这把戏又漂亮又整洁

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)
  }
于 2013-12-22T15:41:25.987 回答