```object Parallel { private val iCPU = Runti...
# announcements
j
Copy code
object 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()
    }
}