package com.Actor import java.io.File import scala.actors.Actor import scala.io.Source /** * author pg * date 2018-12-17 */ //本程序是用scala 原生的actor进行多线程读取目录文件的测试,目录下面总共是1000多一点的文件!逻辑是一个文件启动一个actor class ScalaActor(realpath: String) extends Actor { override def act(): Unit = { // react 或者是receive都可以经过测试react的速度是receive的三倍左右 react { case "file" => { val file = Source.fromFile(realpath) for (line <- file.getLines) { // println(line) } sender ! "文件读取完毕" file.close() } } } } object test { def main(args: Array[String]): Unit = { val time1 = System.currentTimeMillis() println(time1) readFile("D:\\whois\\result\\turn2\\1-10.6G") val time2 = System.currentTimeMillis() println(time2) println(time2 - time1) } def readFile(directory: String): Unit = { val file = new File(directory); var count = 0; if (file.isDirectory) { println("检测到目录") var arr = file.listFiles() for (ar <- arr) { var realpath = ar.getAbsolutePath //每一个文件启动一个线程 val actor = new ScalaActor(realpath) actor.start() //!是异步读取 !?是同步读取 val content: Any = actor !? "file" println(content) } } } }
scala actor 多线程测试
最新推荐文章于 2022-05-21 17:42:48 发布