CodeGym /Java Course /๋ชจ๋“ˆ 2: ์ž๋ฐ” ์ฝ”์–ด /๏ปฟExecutorService ์ธํ„ฐํŽ˜์ด์Šค

๏ปฟExecutorService ์ธํ„ฐํŽ˜์ด์Šค

๋ชจ๋“ˆ 2: ์ž๋ฐ” ์ฝ”์–ด
๋ ˆ๋ฒจ 13 , ๋ ˆ์Šจ 4
์‚ฌ์šฉ ๊ฐ€๋Šฅ

Executor ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

Java 5 ์ด์ „์—๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ชจ๋“  ์ž์ฒด ์ฝ”๋“œ ์Šค๋ ˆ๋“œ ๊ด€๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ,์ƒˆ ์Šค๋ ˆ๋“œ๊ฐœ์ฒด๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์ด๋ฉฐ ๋ชจ๋“  ๊ฒฝ๋Ÿ‰ ์ž‘์—…์— ๋Œ€ํ•ด ์ƒˆ ์Šค๋ ˆ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์ด์น˜์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฌธ์ œ๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ๋“  ๊ฐœ๋ฐœ์ž์—๊ฒŒ ์นœ์ˆ™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ธฐ๋Šฅ์„ Executor ํ”„๋ ˆ์ž„์›Œํฌ๋กœ Java์— ๋„์ž…ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํฐ ์•„์ด๋””์–ด๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ ์ž‘์—…๋งˆ๋‹ค ์ƒˆ ์Šค๋ ˆ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๋Œ€์‹  ์Šค๋ ˆ๋“œ๋ฅผ ์ผ์ข…์˜ "์ €์žฅ์†Œ"์— ๋ณด๊ด€ํ•˜๊ณ  ์ƒˆ ์ž‘์—…์ด ๋„์ฐฉํ•˜๋ฉด ์ƒˆ ์ž‘์—…์„ ๋งŒ๋“œ๋Š” ๋Œ€์‹  ๊ธฐ์กด ์Šค๋ ˆ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

์ด ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์ฃผ์š” ์ธํ„ฐํŽ˜์ด์Šค๋Š” Executor , ExecutorService ๋ฐ ScheduledExecutorService ์ด๋ฉฐ ๊ฐ๊ฐ ์ด์ „ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.

Executor ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ธฐ๋ณธ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. Runnable ๊ฐœ์ฒด ์— ์˜ํ•ด ๊ตฌํ˜„๋˜๋Š” ๋‹จ์ผ ๋ฌดํšจ ์‹คํ–‰(Runnable ๋ช…๋ น) ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค .

ExecutorService ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ€ ๋” ํฅ๋ฏธ๋กญ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์™„๋ฃŒ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ผ์ข…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• ์„ค๋ช…
๋ฌดํšจ ์ข…๋ฃŒ(); ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ExecutorService ๊ฐ€ ์ค‘์ง€๋ฉ๋‹ˆ๋‹ค . ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ด๋ฏธ ์ œ์ถœ๋œ ๋ชจ๋“  ์ž‘์—…์€ ์™„๋ฃŒ๋˜์ง€๋งŒ ์ƒˆ ์ž‘์—…์€ ์ˆ˜๋ฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
List<Runnable> shutdownNow();

์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ExecutorService ๊ฐ€ ์ค‘์ง€๋ฉ๋‹ˆ๋‹ค . ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ด๋ฏธ ์ œ์ถœ๋œ ๋ชจ๋“  ์ž‘์—…์— ๋Œ€ํ•ด Thread.interrupt ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋Œ€๊ธฐ ์ค‘์ธ ์ž‘์—… ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ "์ง„ํ–‰ ์ค‘"์ธ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ฒฝ๊ณ : ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ˆ„์ถœ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ถ€์šธ isShutdown(); ExecutorService ๊ฐ€ ์ค‘์ง€๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค .
๋ถ€์šธ isTerminated(); ExecutorService ์ข…๋ฃŒ ํ›„ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค . shutdown() ๋˜๋Š” shutdownNow()๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๊นŒ์ง€ ํ•ญ์ƒ false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค .
๋ถ€์šธ awaitTermination(long timeout, TimeUnit unit)์€ InterruptedException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

shutdown() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ ๋œ ํ›„ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ฐธ์ผ ๋•Œ๊นŒ์ง€ ํ˜ธ์ถœ๋œ ์Šค๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จํ•ฉ๋‹ˆ๋‹ค.

  • ์˜ˆ์•ฝ๋œ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋œ ์ œํ•œ ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ํ˜„์žฌ ์Šค๋ ˆ๋“œ๊ฐ€ ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ ํ•˜๊ณ  ์ข…๋ฃŒ ์ „์— ์ œํ•œ ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ•˜๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

<T> Future<T> ์ œ์ถœ(Callable<T> ์ž‘์—…);

Callable ์ž‘์—…์„ ExecutorService ์— ์ถ”๊ฐ€ ํ•˜๊ณ  Future ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค .

<T> ๋Š” ์ „๋‹ฌ๋œ ์ž‘์—…์˜ ๊ฒฐ๊ณผ ์œ ํ˜•์ž…๋‹ˆ๋‹ค.

<T> Future<T> ์ œ์ถœ(์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ž‘์—…, T ๊ฒฐ๊ณผ);

Runnable ์ž‘์—…์„ ExecutorService ์— ์ถ”๊ฐ€ ํ•˜๊ณ  Future ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค .

T ๊ฒฐ๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ get() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์— ์˜ํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.๋ฏธ๋ž˜ ๊ฐ์ฒด.

Future<?> ์ œ์ถœ(์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ž‘์—…);

Runnable ์ž‘์—…์„ ExecutorService ์— ์ถ”๊ฐ€ ํ•˜๊ณ  Future ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค .

๊ฒฐ๊ณผ Future ๊ฐ์ฒด์—์„œ get() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด null์„ ์–ป์Šต๋‹ˆ๋‹ค.

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> ์ž‘์—…) throws InterruptedException;

Callable ์ž‘์—… ๋ชฉ๋ก์„ ExecutorService ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค . ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” Future ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ œ์ถœ๋œ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ์ด ๋ชฉ๋ก์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ž‘์—… ์ปฌ๋ ‰์…˜์ด ์ˆ˜์ •๋˜๋ฉด ์ด ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค .

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException;

Callable ์ž‘์—… ๋ชฉ๋ก์„ ExecutorService ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค . ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” Future ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชฉ๋ก์€ ์ „๋‹ฌ๋œ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋˜๊ฑฐ๋‚˜ ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋œ ์ œํ•œ ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ•œ ํ›„ ์ค‘ ๋จผ์ € ๋„๋ž˜ํ•˜๋Š” ์‹œ์ ์— ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ•˜๋ฉด ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ž‘์—…์ด ์ทจ์†Œ๋ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ : ์ทจ์†Œ๋œ ์ž‘์—…์ด ์‹คํ–‰์„ ์ค‘์ง€ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์˜ˆ์ œ์—์„œ ์ด ๋ถ€์ž‘์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ).

๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ž‘์—… ์ปฌ๋ ‰์…˜์ด ์ˆ˜์ •๋˜๋ฉด ์ด ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค .

<T> T invokeAny(Collection<? extends Callable<T>> tasks)๋Š” InterruptedException, ExecutionException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

Callable ์ž‘์—… ๋ชฉ๋ก์„ ExecutorService ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค . ์˜ˆ์™ธ(์žˆ๋Š” ๊ฒฝ์šฐ)๋ฅผ throwํ•˜์ง€ ์•Š๊ณ  ์™„๋ฃŒ๋œ ์ž‘์—…(์žˆ๋Š” ๊ฒฝ์šฐ) ์ค‘ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ž‘์—… ์ปฌ๋ ‰์…˜์ด ์ˆ˜์ •๋˜๋ฉด ์ด ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค .

<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;

Callable ์ž‘์—… ๋ชฉ๋ก์„ ExecutorService ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค . ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋œ ์ œํ•œ ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ•˜๊ธฐ ์ „์— ์˜ˆ์™ธ๋ฅผ throwํ•˜์ง€ ์•Š๊ณ  ์™„๋ฃŒ๋œ ์ž‘์—…(์žˆ๋Š” ๊ฒฝ์šฐ) ์ค‘ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ž‘์—… ์ปฌ๋ ‰์…˜์ด ์ˆ˜์ •๋˜๋ฉด ์ด ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค .

ExecutorService ์ž‘์—…์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค .


import java.util.List;
import java.util.concurrent.*;

public class ExecutorServiceTest {
   public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
//Create an ExecutorService for 2 threads
       java.util.concurrent.ExecutorService executorService = new ThreadPoolExecutor(2, 2, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
// Create 5 tasks
       MyRunnable task1 = new MyRunnable();
       MyRunnable task2 = new MyRunnable();
       MyRunnable task3 = new MyRunnable();
       MyRunnable task4 = new MyRunnable();
       MyRunnable task5 = new MyRunnable();

       final List<MyRunnable> tasks = List.of(task1, task2, task3, task4, task5);
// Pass a list that contains the 5 tasks we created
       final List<Future<Void>> futures = executorService.invokeAll(tasks, 6, TimeUnit.SECONDS);
       System.out.println("Futures received");

// Stop the ExecutorService
       executorService.shutdown();

       try {
           TimeUnit.SECONDS.sleep(3);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

       System.out.println(executorService.isShutdown());
       System.out.println(executorService.isTerminated());
   }

   public static class MyRunnable implements Callable<Void> {

       @Override
       public void call() {
// Add 2 delays. When the ExecutorService is stopped, we will see which delay is in progress when the attempt is made to stop execution of the task
           try {
               TimeUnit.SECONDS.sleep(3);
           } catch (InterruptedException e) {
               System.out.println("sleep 1: " + e.getMessage());
           }
           try {
               TimeUnit.SECONDS.sleep(2);
           } catch (InterruptedException e) {
               System.out.println("sleep 2: " + e.getMessage());
           }
           System.out.println("done");
           return null;
       }
   }
}

์‚ฐ์ถœ:

์™„๋ฃŒ
์™„๋ฃŒ
ํ“จ์ฒ˜๊ฐ€ ์ˆ˜์‹ ํ•œ
์ˆ˜๋ฉด 1: ์ˆ˜๋ฉด ์ค‘๋‹จ
์ˆ˜๋ฉด 1: ์ˆ˜๋ฉด ์ค‘๋‹จ
์™„๋ฃŒ
์™„๋ฃŒ
์ฐธ
์ฐธ

๊ฐ ์ž‘์—…์€ 5์ดˆ ๋™์•ˆ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐœ์˜ ์Šค๋ ˆ๋“œ์— ๋Œ€ํ•œ ํ’€์„ ๋งŒ๋“ค์—ˆ์œผ๋ฏ€๋กœ ์ฒ˜์Œ ๋‘ ์ค„์˜ ์ถœ๋ ฅ์ด ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ด๋ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๊ณ  6์ดˆ ํ›„์— invokeAll ๋ฉ”์„œ๋“œ๊ฐ€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋˜๊ณ  ๊ฒฐ๊ณผ๊ฐ€ Futures ๋ชฉ๋ก์œผ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค . ์ด๊ฒƒ์€ ์ถœ๋ ฅ ๋ฌธ์ž์—ด Futures received ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

์ฒ˜์Œ ๋‘ ๊ฐ€์ง€ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ๋‘ ๊ฐ€์ง€ ์ž‘์—…์ด ๋” ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ invokeAll ๋ฉ”์„œ๋“œ์— ์„ค์ •๋œ ์ œํ•œ ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋‘ ์ž‘์—…์€ ์™„๋ฃŒํ•  ์‹œ๊ฐ„์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋“ค์€ "์ทจ์†Œ" ๋ช…๋ น์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ถœ๋ ฅ์— sleep 1: sleep interrupted ๊ฐ€ ํฌํ•จ๋œ ๋‘ ์ค„์ด ์žˆ๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค .

๊ทธ๋Ÿฐ ๋‹ค์Œ done ์ด ์žˆ๋Š” ๋‘ ์ค„์„ ๋” ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์ด๊ฒƒ์ด invokeAll ๋ฉ”์„œ๋“œ๋ฅผ ์„ค๋ช…ํ•  ๋•Œ ์–ธ๊ธ‰ํ•œ ๋ถ€์ž‘์šฉ์ž…๋‹ˆ๋‹ค .

๋‹ค์„ฏ ๋ฒˆ์งธ์ด์ž ๋งˆ์ง€๋ง‰ ์ž‘์—…์€ ์‹œ์ž‘์กฐ์ฐจ ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ถœ๋ ฅ์— ์•„๋ฌด๊ฒƒ๋„ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰ ๋‘ ์ค„์€ isShutdown ๋ฐ isTerminated ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค .

๋””๋ฒ„๊ทธ ๋ชจ๋“œ์—์„œ ์ด ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์‹œ๊ฐ„ ์ œํ•œ์ด ๊ฒฝ๊ณผํ•œ ํ›„ ์ž‘์—… ์ƒํƒœ๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ๋„ ํฅ๋ฏธ๋กญ์Šต๋‹ˆ๋‹ค( executorService.shutdown(); ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค„์— ์ค‘๋‹จ์ ์„ ์„ค์ • ).

๋‘ ๊ฐœ์˜ ์ž‘์—…์ด ์ •์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋˜๊ณ  ์„ธ ๊ฐœ์˜ ์ž‘์—…์ด "์ทจ์†Œ๋จ" ์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

ScheduledExecutorService

์‹คํ–‰์ž์— ๋Œ€ํ•œ ๋…ผ์˜๋ฅผ ๋งˆ์น˜๊ธฐ ์œ„ํ•ด ScheduledExecutorService ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค .

4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• ์„ค๋ช…
public ScheduledFuture<?> schedule(์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ช…๋ น, ๊ธด ์ง€์—ฐ, TimeUnit ๋‹จ์œ„); ์ „๋‹ฌ๋œ Runnable ์ž‘์—…์ด ์ธ์ˆ˜๋กœ ์ง€์ •๋œ ์ง€์—ฐ ํ›„ ํ•œ ๋ฒˆ ์‹คํ–‰๋˜๋„๋ก ์˜ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค.
public <V> ScheduledFuture<V> schedule(Callable<V> ํ˜ธ์ถœ ๊ฐ€๋Šฅ, ๊ธด ์ง€์—ฐ, TimeUnit ๋‹จ์œ„); ์ „๋‹ฌ๋œ Callable ํƒœ์Šคํฌ๊ฐ€ ์ธ์ˆ˜๋กœ ์ง€์ •๋œ ์ง€์—ฐ ํ›„ ํ•œ ๋ฒˆ ์‹คํ–‰๋˜๋„๋ก ์Šค์ผ€์ค„ํ•ฉ๋‹ˆ๋‹ค.
public ScheduledFuture<?> scheduleAtFixedRate(์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ช…๋ น, ๊ธด initialDelay, ๊ธด ๊ธฐ๊ฐ„, TimeUnit ๋‹จ์œ„); ์ „๋‹ฌ๋œ ์ž‘์—…์˜ ์ฃผ๊ธฐ์  ์‹คํ–‰์„ ์˜ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ initialDelay ์ดํ›„ ์ฒ˜์Œ์œผ๋กœ ์‹คํ–‰๋˜๊ณ  ๊ฐ ํ›„์† ์‹คํ–‰์€ period ์ดํ›„์— ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค .
public ScheduledFuture<?> scheduleWithFixedDelay(์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ช…๋ น, ๊ธด initialDelay, ๊ธด ์ง€์—ฐ, TimeUnit ๋‹จ์œ„); initialDelay ์ดํ›„ ์ฒ˜์Œ์œผ๋กœ ์‹คํ–‰๋  ์ „๋‹ฌ๋œ ์ž‘์—…์˜ ์ฃผ๊ธฐ์  ์‹คํ–‰์„ ์˜ˆ์•ฝ ํ•˜๊ณ  ๊ฐ ํ›„์† ์‹คํ–‰์€ ์ง€์—ฐ (์ด์ „ ์‹คํ–‰ ์™„๋ฃŒ์™€ ํ˜„์žฌ ์‹คํ–‰ ์‹œ์ž‘ ์‚ฌ์ด์˜ ๊ธฐ๊ฐ„) ํ›„์— ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค .
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION