Created
June 5, 2016 06:46
-
-
Save apivovarov/8f83ac75dfe735fffaa4723d4e3d5f7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.x4444.onakka | |
import java.io.InputStream | |
import java.util.concurrent._ | |
/** | |
* RunAll | |
*/ | |
object RunAll { | |
def solve[T](e: Executor, solvers: List[Callable[T]]): List[T] = { | |
val ecs = new ExecutorCompletionService[T](e) | |
solvers.foreach(s => ecs.submit(s)) | |
// get results from All | |
solvers.map { s => | |
ecs.take().get() | |
} | |
} | |
} | |
object Example { | |
def main(args: Array[String]): Unit = { | |
val tpe = Executors.newFixedThreadPool(5) | |
val res = Arm(getClass.getResourceAsStream("/a.txt")).map { is => | |
val solvers = List(MyRead(is), MyRead(is), MyRead(is), | |
MyRead(is), MyRead(is), MyRead(is), MyRead(is)) | |
RunAll.solve(tpe, solvers) | |
} | |
println(res.mkString(",")) | |
} | |
} | |
case class MyRead(is: InputStream) extends Callable[String] { | |
override def call(): String = { | |
val arr = Array.fill[Byte](1)(0) | |
val l = is.read(arr) | |
if (l == -1) null | |
else new String(arr, "UTF-8") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment