case class ST(fName:String) case class RT(re:Map[String,Int])
class Awc extends Actor{ override def act(): Unit = { loop({ react({ case ST(fName) => { val lines = Source.fromFile(fName).mkString val map = lines.split("\t\n").flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size) sender ! RT(map) } }) }) } }
object WordCount {
def main(args: Array[String]): Unit = { val arr = Array("E:\\a.txt","E:\\b.txt") val f = new mutable.ListBuffer[Future[Any]] for (fname <- arr){ val awc = new Awc awc.start() f += awc !! ST(fname) }
val t = new mutable.ListBuffer[RT]
while (f.size > 0){ for (fu <- f){ if (fu.isSet){ val ft = fu.apply() val rt = ft.asInstanceOf[RT] t += rt f -= fu } } } t.flatMap(_.re).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2).reverse.take(20).foreach(println(_)) } }