๊ฐ๋จํ ํ๋ก๊ทธ๋จ์ ๊ณ ๋ คํ์ญ์์ค.
public static void main(String[] args) throws Exception {
// Create an ExecutorService with a fixed number of threads: three
ExecutorService service = Executors.newFixedThreadPool(3);
// Pass a simple Runnable task to the ExecutorService
service.submit(() -> System.out.println("done"));
}
ํ๋ก๊ทธ๋จ์ ์คํํ๋ฉด ๊ธฐ๋ํ๋ ์ฝ์ ์ถ๋ ฅ์ด ์์ฑ๋ฉ๋๋ค.
๊ทธ๋ฌ๋ ์ด๊ฒ์ ์ผ๋ฐ์ ์ผ๋ก IntelliJ IDEA์์ ๋ณผ ์ ์๋ ์ถ๋ ฅ์ด ๋ค๋ฐ๋ฅด์ง ์์ต๋๋ค.
์ฐ๋ฆฌ๋ ๋ณดํต ํ๋ก๊ทธ๋จ์ด ๋๋ ๋ ๊ทธ๊ฒ์ ๋ด ๋๋ค.
์ ๊ทธ๋ฐ ์ผ์ด ๋ฐ์ํฉ๋๊น?
newFixedThreadPool() ๋ฉ์๋ ์ ๋ํ ์ค๋ช ์ ExecutorService ๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ๋ ์ค๋ ๋๊ฐ ๋ช ์์ ์ผ๋ก ์ค์ง๋ ๋๊น์ง ๊ณ์ ์กด์ฌํ๋ค๊ณ ์๋ ค์ค๋๋ค . ์ฆ, ExecutorService ์ ์์ ์ ์ ๋ฌํ๊ธฐ ๋๋ฌธ์ ์์ ์ ์คํํ๊ธฐ ์ํ ์ค๋ ๋๊ฐ ์์ฑ๋์๊ณ ์์ ์ด ์๋ฃ๋ ํ์๋ ํด๋น ์ค๋ ๋๊ฐ ๊ณ์ ์กด์ฌํฉ๋๋ค.
ExecutorService์์ ์ค์ง
๋ฐ๋ผ์ ExecutorService ๋ฅผ "์ข ๋ฃ"(๋๋ ์ค์ง)ํด์ผ ํฉ๋๋ค . ์ฐ๋ฆฌ๋ ์ด๊ฒ์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ํ ์ ์์ต๋๋ค:
-
void shutdown() โ ์ด ๋ฉ์๋๊ฐ ํธ์ถ๋ ํ ExecutorService๋ ์ ์์ ์๋ฝ์ ์ค์งํฉ๋๋ค. ์ด์ ์ ExecutorService ์ ์ ์ถ๋ ๋ชจ๋ ์์ ์ ๊ณ์ ์คํ๋ฉ๋๋ค.
public static void main(String[] args) throws Exception { ExecutorService service = Executors.newFixedThreadPool(3); service.submit(() -> System.out.println("task 1")); service.submit(() -> System.out.println("task 2")); service.shutdown(); // A RejectedExecutionException will occur here service.submit(() -> System.out.println("task 3")); }
-
List<Runnable> shutdownNow() โ ์ด ๋ฉ์๋๋ ํ์ฌ ํ์ฑํ๋ ์์ ์ ์ค์งํ๋ ค๊ณ ์๋ํฉ๋๋ค. ์ฌ์ ํ ์์ ์ ์ฐจ๋ก๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ณ ์๋ ์์ ์ ํ๊ธฐ๋๊ณ Runnables ๋ชฉ๋ก์ผ๋ก ๋ฐํ๋ฉ๋๋ค .
public static void main(String[] args) throws Exception { ExecutorService service = Executors.newFixedThreadPool(5); List.of(1, 2, 3, 4, 5, 6, 7, 8).forEach(i -> service.submit(() -> System.out.println(i))); List<Runnable> runnables = service.shutdownNow(); runnables.forEach(System.out::println); }
์ฐ์ถ:
2
4
3
java.util.concurrent.FutureTask@1e80bfe8[์๋ฃ๋์ง ์์, ์์ = java.util.concurrent.Executors$RunnableAdapter@4edde6e5[ํฌ์ฅ๋ ์์ = Test$$Lambda$16/0x0000000800b95040@70177ecd]]
java.util.concurrent .FutureTask@cc34f4d[์๋ฃ๋์ง ์์, ์์ = java.util.concurrent.Executors$RunnableAdapter@66a29884[ํฌ์ฅ๋ ์์ = Test$$Lambda$16/0x0000000800b95040@4769b07b]]
java.util.concurrent.FutureTask@6f539caf[์๋ฃ๋์ง ์์, ์์ = java.util.concurrent.Executors$RunnableAdapter@17a7cec2[Wrapped task = Test$$Lambda$16/0x0000000800b95040@65b3120a]]
5
ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ ์ฝ๋ 0์ผ๋ก ์๋ฃ๋จ
์ถ๋ ฅ์ ์คํ๋ง๋ค ๋ค๋ฆ ๋๋ค. ์ถ๋ ฅ์๋ ๋ ์ข ๋ฅ์ ๋ผ์ธ์ด ์์ต๋๋ค.
-
์ซ์๋ ExecutorService ๊ฐ ํด๋น ์์ ์ ์ฒ๋ฆฌํ์์ ์๋ฏธํ๋ฉฐ ์์ ์ ์์ฑํ๋ ๋ฐ ์ฌ์ฉํ ๋ชฉ๋ก์ ๋ฒํธ๋ฅผ ํ์ํฉ๋๋ค.
-
FutureTask ๊ฐ์ฒด ์์ toString() ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ์ ๋๋ค . ์ด๋ฌํ ๊ฐ์ฒด๋ ExecutorService ์ ์ ์ถ๋์์ง๋ง ์ฒ๋ฆฌ๋์ง ์์ ์์ ์ ๋๋ค .
์ถ๋ ฅ์๋ ๋ ๋ค๋ฅธ ํฅ๋ฏธ๋ก์ด ๋์์ค๊ฐ ์์ต๋๋ค. ์ด์์ ์ธ ์ธ์์์๋ ํ์๋ ๋ชจ๋ ์ซ์๋ฅผ ๋จผ์ ๋ณธ ๋ค์ FutureTask ๊ฐ์ฒด๋ฅผ ๋ณผ ๊ฒ์ ๋๋ค. ๊ทธ๋ฌ๋ ๋๊ธฐํ ๋ฌธ์ ๋ ์ถ๋ ฅ์ ๋ผ์ธ์ ๋ค์ฃฝ๋ฐ์ฃฝ์ผ๋ก ๋ง๋ญ๋๋ค.
๋ค๋ฅธ ๋ฐฉ๋ฒ
ExecutorService์๋ ์ค์ง์ ๊ด๋ จ๋ ๋ช ๊ฐ์ง ์ถ๊ฐ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
-
boolean awaitTermination(long timeout, TimeUnit unit) โ ์ด ๋ฉ์๋๋ ์์ ์ ํธ์ถํ๋ ์ค๋ ๋๋ฅผ ์ฐจ๋จํฉ๋๋ค. ๋ค์ ์ธ ๊ฐ์ง ์ด๋ฒคํธ ์ค ํ๋๊ฐ ๋ฐ์ํ๋ ์ฆ์ ์ฐจ๋จ์ด ์ข ๋ฃ๋ฉ๋๋ค.
- shutdown() ๋ฉ์๋๊ฐ ํธ์ถ๋ ํ ๋ชจ๋ ํ์ฑ ์์ ๊ณผ ์์ฝ๋ ์์ ์ด ๋ชจ๋ ์คํ๋์์ต๋๋ค.
- ๋ฉ์๋ ๋งค๊ฐ ๋ณ์์ ์ํด ๊ฒฐ์ ๋ ์๊ฐ ์ ํ์ด ๊ฒฝ๊ณผํ์ต๋๋ค.
- awaitTermination() ๋ฉ์๋ ๋ฅผ ํธ์ถํ ์ค๋ ๋๊ฐ ์ข ๋ฃ๋ฉ๋๋ค.
์ด ๋ฉ์๋๋ ์ ํ ์๊ฐ์ด ๊ฒฝ๊ณผํ๊ธฐ ์ ์ ExecutorService๊ฐ ์ค์ง๋ ๊ฒฝ์ฐ true๋ฅผ ๋ฐํ ํ๊ณ ์ ํ ์๊ฐ์ด ์ด๋ฏธ ๊ฒฝ๊ณผํ ๊ฒฝ์ฐ false๋ฅผ ๋ฐํ ํฉ๋๋ค.
public static void main(String[] args) throws Exception { ExecutorService service = Executors.newFixedThreadPool(2); service.submit(() -> System.out.println("task 1")); service.submit(() -> System.out.println("task 2")); service.submit(() -> System.out.println("task 3")); service.shutdown(); System.out.println(service.awaitTermination(1, TimeUnit.MICROSECONDS)); }
-
boolean isShutdown() โ ExecutorService ์์ shutdown() ๋๋ shutdownNow() ๋ฉ์๋๊ฐ ํธ์ถ๋ ๊ฒฝ์ฐ true๋ฅผ ๋ฐํํฉ๋๋ค .
public static void main(String[] args) throws Exception { ExecutorService service = Executors.newFixedThreadPool(2); service.submit(() -> System.out.println("task 1")); service.submit(() -> System.out.println("task 2")); service.submit(() -> System.out.println("task 3")); System.out.println(service.isShutdown()); service.shutdown(); System.out.println(service.isShutdown()); }
-
boolean isTerminated() โ ExecutorService ์์ shutdown () ๋๋ shutdownNow() ๋ฉ์๋๊ฐ ํธ์ถ๋๊ณ ๋ชจ๋ ์์ ์ด ์๋ฃ๋๋ฉด true๋ฅผ ๋ฐํํฉ๋๋ค.
public static void main(String[] args) throws Exception { ExecutorService service = Executors.newFixedThreadPool(5); List.of(1, 2, 3, 4, 5, 6, 7, 8).forEach(i -> service.submit(() -> System.out.println(i))); service.shutdownNow(); System.out.println(service.isTerminated()); }
๋ค์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ์์ ์ฝ๋:
public static void main(String[] args) throws Exception {
ExecutorService service = Executors.newFixedThreadPool(16);
Callable<String> task = () -> {
Thread.sleep(1);
return "Done";
};
// Add 10,000 tasks to the queue
List<Future<String>> futures = IntStream.range(0, 10_000)
.mapToObj(i -> service.submit(task))
.collect(Collectors.toList());
System.out.printf("%d tasks were submitted for execution.%n", futures.size());
// Attempt to shut down
service.shutdown();
// Wait 100 milliseconds to finish the work
if (service.awaitTermination(100, TimeUnit.MILLISECONDS)) {
System.out.println("All tasks completed!");
} else {
// Stop forcibly
List<Runnable> notExecuted = service.shutdownNow();
System.out.printf("%d tasks were not started.%n", notExecuted.size());
}
System.out.printf("Total tasks completed: %d.%n", futures.stream().filter(Future::isDone).count());
}
์ถ๋ ฅ(์คํ๋ง๋ค ๋ค๋ฆ):
9170 ์์ ์ด ์์๋์ง ์์์ต๋๋ค.
์๋ฃ๋ ์ด ์์ : 830๊ฐ ์์ .
์ข ๋ฃ ์ฝ๋ 0์ผ๋ก ํ๋ก์ธ์ค๊ฐ ์๋ฃ๋์์ต๋๋ค.
GO TO FULL VERSION