From 0b6ea7364cb2a76dcfc2685d7a5029438d1ed7f0 Mon Sep 17 00:00:00 2001 From: 7094vv4 <24241035+7094vv4@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:04:43 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8C=91=E6=88=A6=20=E5=88=9D=E7=B4=9A?= =?UTF-8?q?=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 | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/scala/TenThousandNamePrinter.scala b/src/main/scala/TenThousandNamePrinter.scala index c31ee74..2b3fbcf 100644 --- a/src/main/scala/TenThousandNamePrinter.scala +++ b/src/main/scala/TenThousandNamePrinter.scala @@ -1,10 +1,16 @@ +import java.util.concurrent.Executors + object TenThousandNamePrinter extends App { + val es = Executors.newCachedThreadPool() for (i <- 1 to 10000) { - new Thread(() => { - Thread.sleep(1000) - println(Thread.currentThread().getName) - }).start() + es.submit(new Runnable { + override def run(): Unit = { + Thread.sleep(1000) + println(s"${Thread.currentThread().getName} + $i") + } + }) } + es.shutdown() } \ No newline at end of file