Thread μ€ν
β’
Thread λ₯Ό extends νλ€.
β’
Runnable μ implements νλ€.
β’
λ λ€ run() μ override νκ³ Thread κ°μ²΄μ start() λ₯Ό νΈμΆν΄μΌ νλ€.
β’
start() μ join() μ μ°¨μ΄λ 무μμΌκΉ?
Thread Pool
β’
in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.
β¦
λκ·λͺ¨ μ΄ν리μΌμ΄μ
μμλ ν λΉκ³Ό μ κ±°κ° λ©λͺ¨λ¦¬ κ΄λ¦¬ μ€λ²ν€λλ₯Ό μ΄λνλ€.
β’
미리 μ°λ λλ₯Ό μΌμ μλ§νΌ μ€μ ν΄ λλ κ²
β’
Executors μ ExecutorService λ‘ μ€μ κ°λ₯
β’
Executors.newFixedThreadPool(int nThreads):
β¦
This type of pool always has a specified number of threads running
β¦
if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread.
β¦
Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.
β¦
μ₯μ : μ€λ²ν€λλ₯Ό λ§μμ€ μ μλ€.
β¦
nκ° λ§νΌμ μ°λ λλ₯Ό μμ±
β’
Executors.newCachedThreadPool()
β¦
μ΄κΈ° μ°λ λ κ°μλ 0, κ°μλ³΄λ€ λ§μ μμ μμ
μ΄ μμ²λλ©΄ μλ‘μ΄ μ°λ λλ₯Ό μμ±νμ¬ μμ
μ²λ¦¬
β¦
μμ
μ΄ λλ μ°λ λκ° 60μ΄ λμ μλ‘μ΄ μμ
μμ²μ΄ μμΌλ©΄ μ°λ λλ₯Ό μ’
λ£νκ³ μ°λ λ νμμ μ κ±°
start(), join()
submit(), execute()
execute()
β’
μμ
μ²λ¦¬ κ²°κ³Όλ₯Ό λ°ννμ§ μμ΅λλ€.
β’
μμ
μ²λ¦¬ λμ€ μμΈ λ°μνλ©΄ μ€λ λκ° μ’
λ£λκ³ ν΄λΉ μ€λ λλ μ€λ λνμμ μ κ±°νκ³ , μ€λ λνμ μμ
μ²λ¦¬λ₯Ό μν΄ μλ‘μ΄ μ€λ λλ₯Ό μμ±ν©λλ€.
submit()
β’
μμ
μ²λ¦¬ κ²°κ³Όλ₯Ό λ°νν©λλ€.
β’
μμ
μ²λ¦¬ λμ€ μμΈκ° λ°μνλλΌλ μ€λ λλ μ’
λ£λμ§ μκ³ λ€μ μμ
μ μν΄Β μ¬μ¬μ©λ©λλ€.