diff --git a/src/main/scala/TenThousandNamePrinter.scala b/src/main/scala/TenThousandNamePrinter.scala index c31ee74..24eea87 100644 --- a/src/main/scala/TenThousandNamePrinter.scala +++ b/src/main/scala/TenThousandNamePrinter.scala @@ -1,10 +1,17 @@ +import java.util.concurrent.Executors + object TenThousandNamePrinter extends App { + val executors = Executors.newFixedThreadPool(10); - for (i <- 1 to 10000) { - new Thread(() => { - Thread.sleep(1000) - println(Thread.currentThread().getName) - }).start() + val futures = for (i <- 1 to 10000) yield { + executors.submit(new Runnable { + override def run(): Unit = { + Thread.sleep(1000) + println(Thread.currentThread().getName) + } + }) } + futures.foreach(f => f.get()) + executors.shutdown() } \ No newline at end of file