Joey Heck
04/05/2018, 6:19 PMobject Parallel {
private val iCPU = Runtime.getRuntime().availableProcessors()
interface LoopBody<T> {
fun run(i: T)
}
fun For(start: Int, stop: Int, loopBody: LoopBody<Int>) {
val executor = Executors.newFixedThreadPool(iCPU)
val futures = LinkedList<Future<*>>()
for (i in start until stop) {
val future = executor.submit { loopBody.run(i) }
futures.add(future)
}
for (f in futures) {
try {
f.get()
} catch (e: InterruptedException) {
} catch (e: ExecutionException) {
}
}
executor.shutdown()
}
}