From 983de854574f4e137171fe097941e18b7abbf998 Mon Sep 17 00:00:00 2001 From: haru221 Date: Thu, 17 May 2018 22:48:34 +0900 Subject: [PATCH] =?UTF-8?q?Executor=20=E3=83=95=E3=83=AC=E3=83=BC=E3=83=A0?= =?UTF-8?q?=E3=83=AF=E3=83=BC=E3=82=AF=E3=81=AE=E6=8C=91=E6=88=A6=E5=95=8F?= =?UTF-8?q?=E9=A1=8C=E3=81=AE=E8=A7=A3=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/scala/TenThousandNamePrinter.scala | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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