Skip to content

Latest commit

Β 

History

History
79 lines (67 loc) Β· 3.97 KB

README.md

File metadata and controls

79 lines (67 loc) Β· 3.97 KB

Thread Pool

  • Process

    μš΄μ˜μ²΄μ œμ—μ„œ ν•˜λ‚˜μ˜ μ• ν”Œλ¦¬μΌ€μ΄μ…˜

  • Thread

    Process μ—μ„œ ν•˜λ‚˜μ˜ μž‘μ—…

μ—¬λŸ¬ Thread λ₯Ό λ™μ‹œμ— λ§Œλ“€μ–΄ μ‹€ν–‰(λ³‘λ ¬μ²˜λ¦¬)ν•  수 μžˆλ‹€.
Java 의 경우 Thread, Runnableλ₯Ό μ΄μš©ν•΄μ•Ό ν•œλ‹€.

λ™μ‹œμ„±(Concurrency)와 병렬성(Parallelism)

  • λ™μ‹œμ„±μ€ μ‹±κΈ€ μ½”μ–΄μ—μ„œ λ©€ν‹° μŠ€λ ˆλ“œλ₯Ό λ™μž‘μ‹œν‚€κΈ° μœ„ν•œ 방식

    λ©€ν‹° νƒœμŠ€ν‚Ήμ„ μœ„ν•΄ μ—¬λŸ¬ 개의 μŠ€λ ˆλ“œκ°€ λ²ˆκ°ˆμ•„κ°€λ©΄μ„œ μ‹€ν–‰λ˜λŠ” μ„±μ§ˆμ„ λ§ν•œλ‹€.
    λ™μ‹œμ„±μ„ μ΄μš©ν•œ μ‹±κΈ€ μ½”μ–΄μ˜ λ©€ν‹° νƒœμŠ€ν‚Ήμ€ 각 μŠ€λ ˆλ“œλ“€μ΄ λ³‘λ ¬μ μœΌλ‘œ μ‹€ν–‰λ˜λŠ” κ²ƒμ²˜λŸΌ λ³΄μ΄μ§€λ§Œ,
    사싀은 λ²ˆκ°ˆμ•„κ°€λ©΄μ„œ μ‘°κΈˆμ”© μ‹€ν–‰λ˜κ³  μžˆλŠ” 것이닀.

  • 병렬성은 λ©€ν‹° μ½”μ–΄μ—μ„œ λ©€ν‹° μŠ€λ ˆλ“œλ₯Ό λ™μž‘μ‹œν‚€λŠ” 방식

    1 개 μ΄μƒμ˜ μŠ€λ ˆλ“œλ₯Ό ν¬ν•¨ν•˜λŠ” 각 코어듀이 λ™μ‹œμ— μ‹€ν–‰λ˜λŠ” μ„±μ§ˆμ„ λ§ν•œλ‹€.
    병렬성은 병렬성(Data Parallelism)κ³Ό μž‘μ—… 병렬성(Task Parallelism)으둜 κ΅¬λΆ„λœλ‹€.

데이터 병렬성

  • 전체 데이터λ₯Ό μͺΌκ°œ μ„œλΈŒ λ°μ΄ν„°λ“€λ‘œ λ§Œλ“  λ’€, μ„œλΈŒ 데이터듀을 병렬 μ²˜λ¦¬ν•˜μ—¬ μž‘μ—…μ„ λΉ λ₯΄κ²Œ μˆ˜ν–‰ν•˜λŠ” 것을 λ§ν•œλ‹€.

    Java 8μ—μ„œ μ§€μ›ν•˜λŠ” 병렬 슀트림(Parallelism Stream) 이 데이터 병렬성을 κ΅¬ν˜„ν•œ 것이닀.
    μ„œλΈŒ λ°μ΄ν„°λŠ” λ©€ν‹° μ½”μ–΄μ˜ 수만큼 μͺΌκ°œμ–΄ 각각의 데이터듀을 λΆ„λ¦¬λœ μŠ€λ ˆλ“œμ—μ„œ 병렬 μ²˜λ¦¬ν•œλ‹€.

μž‘μ—… 병렬성

  • μ„œλ‘œ λ‹€λ₯Έ μž‘μ—…μ„ 병렬 μ²˜λ¦¬ν•˜λŠ” 것을 λ§ν•œλ‹€.

    λŒ€ν‘œμ μΈ μ˜ˆλŠ” μ›Ή μ„œλ²„κ°€ μžˆλ‹€. 각각의 λΈŒλΌμš°μ €μ—μ„œ μš”μ²­ν•œ λ‚΄μš©μ„ κ°œλ³„ μŠ€λ ˆλ“œμ—μ„œ λ³‘λ ¬λ‘œ μ²˜λ¦¬ν•œλ‹€.


Thread Pool ?

  • κ·Έλ ‡λ‹€κ³  ν•΄μ„œ Thread λ₯Ό 계속 λŠ˜λ €κ°€λŠ” 것은 μ’‹μ€κ²Œ μ•„λ‹ˆλ‹€.
  • ν•˜λ“œμ›¨μ–΄μ˜ μ œν•œμ μΈ 사항(CPU, Memory λ“±)이 있기 λ•Œλ¬Έμ— 관리할 ν•„μš”κ°€ μžˆλ‹€.
  • κ·Έλž˜μ„œ Thread Pool μ΄λΌλŠ” κ°œλ…μ„ μ΄μš©ν•œλ‹€.

    Thread Pool 은 μž‘μ—… μ²˜λ¦¬μ— μ‚¬μš©λ˜λŠ” μŠ€λ ˆλ“œλ₯Ό μ œν•œλœ 개수만큼 μ •ν•΄ 놓고,
    μž‘μ—… 큐(Queue)에 λ“€μ–΄μ˜€λŠ” μž‘μ—…λ“€μ„ ν•˜λ‚˜μ”© μŠ€λ ˆλ“œκ°€ λ§‘μ•„ μ²˜λ¦¬ν•˜λŠ” 것을 λ§ν•œλ‹€.
    Java 의 경우, 기본적으둜 Executors, ExecutorServiceλ₯Ό μ΄μš©ν•˜μ—¬ Thread Pool 을 λ§Œλ“€ 수 μžˆλ‹€.

Single Thread Pool

ExecutorService executorService = Executors.newSingleThreadExecutor(); 
  • SingleThreadApplication

    단일 Worker Thread
    μ‹€νŒ¨ μ‹œ, μƒˆλ‘œμš΄ Thread λ₯Ό μƒμ„±ν•˜μ§€ μ•ŠμŒ

Fixed Thread Pool

ExecutorService executorService = Executors.newFixedThreadPool(int nThreads);
  • FixedThreadApplication

    κ³ μ •λœ Worker Thread λ₯Ό μ‚¬μš©ν•˜λ©°, λͺ¨λ“  Thread λ₯Ό μ‚¬μš© 쀑이라면 Queue 에 μŒ“μ—¬ λŒ€κΈ°
    μ‹€νŒ¨ μ‹œ, μƒˆλ‘œμš΄ Thread λ₯Ό μƒμ„±ν•˜μ—¬ λŒ€μ²΄

Cached Thread Pool

ExecutorService executorService = Executors.newCachedThreadPool();
  • CachedThreadApplication

    ν•„μš”μ— 따라 μƒˆλ‘œμš΄ Thread λ₯Ό μƒμ„±ν•˜λ©°, 이전에 μƒμ„±λœ Thread κ°€ μ‘΄μž¬ν•˜λ©΄ μž¬μ‚¬μš©
    기본적으둜 60초 정도 Thread μœ μ§€
    비동기 μž‘μ—…μ— 합리적

Scheduler Thread Pool

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(int corePoolSize);
  • ScheduledThreadApplication

    μ§€μ •λœ delay 후에 μ‹€ν–‰ν•˜κ±°λ‚˜, 주기적으둜 μ‹€ν–‰ν•˜λ„λ‘ λͺ…령을 μ˜ˆμ•½

Work Stealing Thread Pool

ExecutorService executorService = Executors.newWorkStealingPool(int parallelism);
  • WorkStealingThreadApplication

    Java 8μ—μ„œ μƒˆλ‘œ 생긴 Thread Pool
    μ§€μ •λœ parallelism 을 μ§€μ›ν• λ§ŒνΌ μΆ©λΆ„ν•œ Thread λ₯Ό μœ μ§€ν•˜κ³  μ—¬λŸ¬ Queue λ₯Ό μ‚¬μš©ν•˜μ—¬ 경합을 μ€„μž„
    Thread λ₯Ό λ™μ μœΌλ‘œ 늘리고 μ€„μž„
    μž‘μ—…μ΄ μ‹€ν–‰λ˜λŠ” μˆœμ„œλ₯Ό 보μž₯ν•˜μ§€ μ•ŠμŒ