Executors ํด๋์ค ์ newFixedThreadPool ๋ฉ์๋ ๋ ๊ณ ์ ๋ ์์ ์ค๋ ๋๋ก executorService๋ฅผ ์์ฑํฉ๋๋ค . newSingleThreadExecutor ๋ฉ์๋ ์ ๋ฌ๋ฆฌ ํ์์ ์ํ๋ ์ค๋ ๋ ์๋ฅผ ์ง์ ํฉ๋๋ค. ๋ด๋ถ์ ์ผ๋ก๋ ๋ค์ ์ฝ๋๊ฐ ํธ์ถ๋ฉ๋๋ค.
new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
corePoolSize ( ์คํ๊ธฐ ์๋น์ค๊ฐ ์์๋ ๋ ์ค๋น(์์)๋ ์ค๋ ๋ ์ ) ๋ฐ maximumPoolSize ( ์คํ๊ธฐ ์๋น์ค๊ฐ ์์ฑํ ์ ์๋ ์ต๋ ์ค๋ ๋ ์ ) ๋งค๊ฐ๋ณ์๋ ๋์ผํ ๊ฐ์ ๋ฐ์ต๋๋ค. newFixedThreadPool (nThreads ) . ๊ทธ๋ฆฌ๊ณ ์ ํํ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฐ๋ฆฌ ์์ ์ ThreadFactory ๊ตฌํ์ ์ ๋ฌํ ์ ์์ต๋๋ค.
์, ExecutorService ๊ฐ ํ์ํ ์ด์ ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค .
๊ณ ์ ๋ ์(n)์ ์ค๋ ๋๊ฐ ์๋ ExecutorService ์ ๋ ผ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค .
- ์์ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ต๋ n๊ฐ์ ์ค๋ ๋๊ฐ ํ์ฑํ๋ฉ๋๋ค.
- n๊ฐ ์ด์์ ์์ ์ด ์ ์ถ๋๋ฉด ์ค๋ ๋๊ฐ ํด์ ๋ ๋๊น์ง ๋๊ธฐ์ด์ ๋ณด๊ด๋ฉ๋๋ค.
- ์ค๋ ๋ ์ค ํ๋๊ฐ ์คํจํ๊ณ ์ข ๋ฃ๋๋ฉด ๊ทธ ์๋ฆฌ๋ฅผ ๋์ ํ ์ ์ค๋ ๋๊ฐ ์์ฑ๋ฉ๋๋ค.
- ํ์ ๋ชจ๋ ์ค๋ ๋๋ ํ์ด ์ข ๋ฃ๋ ๋๊น์ง ํ์ฑ ์ํ์ ๋๋ค.
์๋ฅผ ๋ค์ด ๊ณตํญ์์ ๋ณด์ ๊ฒ์๋๋ฅผ ํต๊ณผํ๊ธฐ ์ํด ๊ธฐ๋ค๋ฆฌ๋ ๊ฒ์ ์์ํด ๋ณด์ญ์์ค. ๋ณด์ ๊ฒ์ ์ง์ ๊น์ง ๋ชจ๋ ์ฌ๋์ด ํ ์ค์ ์ ์์ผ๋ฉฐ ์น๊ฐ์ ๋ชจ๋ ์์ ๊ฒ๋ฌธ์์ ๋ถ์ฐ๋ฉ๋๋ค. ์ฒดํฌํฌ์ธํธ ์ค ํ๋์์ ์ง์ฐ์ด ๋ฐ์ํ๋ฉด ์ฒซ ๋ฒ์งธ ์ฒดํฌํฌ์ธํธ๊ฐ ๋น์์ง ๋๊น์ง ๋ ๋ฒ์งธ ์ฒดํฌํฌ์ธํธ๋ง ๋๊ธฐ์ด์ ์ฒ๋ฆฌํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ๋์ ๊ฒ๋ฌธ์๊ฐ ์์ ํ ๋ซํ๋ฉด ๋ค๋ฅธ ๊ฒ๋ฌธ์๊ฐ ์ด๋ฆฌ๋ฉด์ ์ด๋ฅผ ๋์ฒดํ๊ณ ์น๊ฐ์ ๋ ๊ฒ๋ฌธ์๋ฅผ ํตํด ๊ณ์ ์ฒ๋ฆฌ๋ฉ๋๋ค.
์ฐ๋ฆฌ๋ ์กฐ๊ฑด์ด ์ด์์ ์ด๋ผ ํ ์ง๋ผ๋ โ ์ฝ์๋ n๊ฐ์ ์ค๋ ๋๊ฐ ์์ ์ ์ผ๋ก ์๋ํ๊ณ , ์ค๋ฅ๋ก ๋๋๋ ์ค๋ ๋๋ ํญ์ ๊ต์ฒด๋จ(์ ํ๋ ๋ฆฌ์์ค๋ก ์ธํด ์ค์ ๊ณตํญ์์๋ ๋ฌ์ฑํ ์ ์์) โ ์์คํ ์ ์ฌ์ ํ ์ฌ๋ฌ ๊ฐ์ ์ค๋ ๋๊ฐ ์์ ์ ์ฒ๋ฆฌํ ์ ์๋ ๊ฒ๋ณด๋ค ๋๊ธฐ์ด์ด ๋ ๋นจ๋ฆฌ ์ฆ๊ฐํ๋๋ผ๋ ์ด๋ค ์ํฉ์์๋ ๋ ๋ง์ ์ค๋ ๋๊ฐ ์์ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
ExecutorService๊ฐ ๊ณ ์ ๋ ์์ ์ค๋ ๋์์ ์๋ํ๋ ๋ฐฉ์์ ์ค์ง์ ์ผ๋ก ์ดํดํ๋ ๊ฒ์ด ์ข์ต๋๋ค . Runnable์ ๊ตฌํํ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ด ์๋ค . ์ด ํด๋์ค์ ๊ฐ์ฒด๋ ExecutorService ์ ๋ํ ์์ ์ ๋ํ๋ ๋๋ค .
public class Task implements Runnable {
int taskNumber;
public Task(int taskNumber) {
this.taskNumber = taskNumber;
}
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Processed user request #" + taskNumber + " on thread " + Thread.currentThread().getName());
}
}
run() ๋ฉ์๋ ์์ ์ฐ๋ฆฌ๋ 2์ด ๋์ ์ค๋ ๋๋ฅผ ์ฐจ๋จํ๊ณ ์ผ๋ถ ์ํฌ๋ก๋๋ฅผ ์๋ฎฌ๋ ์ด์ ํ ๋ค์ ํ์ฌ ์์ ์ ๋ฒํธ์ ์์ ์ ์คํํ๋ ์ค๋ ๋์ ์ด๋ฆ์ ํ์ํฉ๋๋ค.
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 30; i++) {
executorService.execute(new Task(i));
}
executorService.shutdown();
๋จผ์ ๊ธฐ๋ณธ ๋ฉ์๋์์ ExecutorService๋ฅผ ๋ง๋ค๊ณ ์คํ์ ์ํด 30๊ฐ์ ์์ ์ ์ ์ถํฉ๋๋ค.
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ
๋ ์ฌ์ฉ์ ์์ฒญ #0 pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #2
pool-์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #5 1-thread-3 ์ค๋ ๋
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #3
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #4 pool-1
-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #8
์ฒ๋ฆฌ๋ ์ฌ์ฉ์ pool-1-thread-3 ์ค๋ ๋์์ ์์ฒญ #6 pool
-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #7 pool-1-thread
-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #10
pool-1-์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #9 thread-1 ์ค๋ ๋
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #11
pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #12
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #14
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #13 pool
-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #15
pool-์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #16 1-thread-2 ์ค๋ ๋
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #17
pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #18
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #19
์ฒ๋ฆฌ๋ ์ฌ์ฉ์ pool-1-thread-1 ์ค๋ ๋์์
์์ฒญ #20 pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #21 pool-
1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #22 pool-1-
์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #23 thread-1 ์ค๋ ๋
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #25
pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #24
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #26
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #27 pool-
1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #28 pool-1-thread-3 ์ค๋ ๋์์
์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #29 1-์ค๋ ๋-1 ์ค๋ ๋
์ฝ์ ์ถ๋ ฅ์ ์์ ์ด ์ด์ ์์ ์ ์ํด ํด์ ๋ ํ ๋ค๋ฅธ ์ค๋ ๋์์ ์คํ๋๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
์ด์ ์์ ์๋ฅผ 100๊ฐ๋ก ๋๋ฆฌ๊ณ 100๊ฐ ์์ ์ ์ ์ถํ ํ awaitTermination (11, SECONDS) ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค. ์ซ์์ ์๊ฐ ๋จ์๋ฅผ ์ธ์๋ก ์ ๋ฌํฉ๋๋ค. ์ด ๋ฐฉ๋ฒ์ ๋ฉ์ธ ์ค๋ ๋๋ฅผ 11์ด ๋์ ์ฐจ๋จํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ shutdownNow()๋ฅผ ํธ์ถํ์ฌ ๋ชจ๋ ์์ ์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ExecutorService๋ฅผ ๊ฐ์ ๋ก ์ข ๋ฃํฉ๋๋ค.
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 100; i++) {
executorService.execute(new Task(i));
}
executorService.awaitTermination(11, SECONDS);
executorService.shutdownNow();
System.out.println(executorService);
๋ง์ง๋ง์๋ executorService ์ ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ํ์ํฉ๋๋ค .
์ฐ๋ฆฌ๊ฐ ์ป๋ ์ฝ์ ์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
0 pool-1-thread-3 ์ค๋ ๋์์
์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #2 pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #1 ํ-1-thread-2 ์ค๋ ๋์์
์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #4 1-thread-3 ์ค๋ ๋
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #5
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #3
pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #6
์ฒ๋ฆฌ๋ ์ฌ์ฉ์ pool-1-thread-2 ์ค๋ ๋์์ ์์ฒญ #7 pool-
1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #8 pool-1
-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #9
pool-1-์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #11 thread-1 ์ค๋ ๋
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #10
pool-1-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #13
pool-1-thread-2 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #14
pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #12
java.util.concurrent.ThreadPoolExecutor@452b3a41[์ข ๋ฃ ์ค, ํ ํฌ๊ธฐ = 3, ํ์ฑ ์ค๋ ๋ = 3 , ๋๊ธฐ ์ค์ธ ์์ = 0, ์๋ฃ๋ ์์ = 15]
pool-1-thread-3 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #17 pool-1
-thread-1 ์ค๋ ๋์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #15
pool-1-thread์์ ์ฒ๋ฆฌ๋ ์ฌ์ฉ์ ์์ฒญ #16 -2 ์ค๋ ๋
๊ทธ ๋ค์์๋ 3๊ฐ์ ํ์ฑ ์์ ์์ ์ ์ ๋ฉ์๋ ์ ์ํด ๋ฐ์ํ 3๊ฐ์ InterruptedExceptions๊ฐ ๋ค๋ฐ๋ฆ ๋๋ค .
ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋๋ฉด 15๊ฐ์ ์์ ์ด ์๋ฃ๋์ง๋ง ํ์๋ ์ฌ์ ํ ์์ ์คํ์ ์๋ฃํ์ง ์์ 3๊ฐ์ ํ์ฑ ์ค๋ ๋๊ฐ ์์์ ์ ์ ์์ต๋๋ค. ์ด ์ธ ์ค๋ ๋์์ interrupt () ๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค. ์ฆ, ์์ ์ด ์๋ฃ๋์ง๋ง ์ฐ๋ฆฌ์ ๊ฒฝ์ฐ sleep ๋ฉ์๋๋ InterruptedException ์ ๋ฐ์์ํต๋๋ค . ๋ํ shutdownNow() ๋ฉ์๋๊ฐ ํธ์ถ๋ ํ ์์ ๋๊ธฐ์ด์ด ์ง์์ง๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
๋ฐ๋ผ์ ํ์์ ๊ณ ์ ๋ ์์ ์ค๋ ๋์ ํจ๊ป ExecutorService๋ฅผ ์ฌ์ฉํ ๋ ์๋ ๋ฐฉ์์ ๊ธฐ์ตํด์ผ ํฉ๋๋ค. ์ด ์ ํ์ ์ผ์ ํ ๋ถํ๊ฐ ์๋ ค์ง ์์ ์ ์ ํฉํฉ๋๋ค.
์ฌ๊ธฐ์ ๋ ๋ค๋ฅธ ํฅ๋ฏธ๋ก์ด ์ง๋ฌธ์ด ์์ต๋๋ค. ๋จ์ผ ์ค๋ ๋์ ๋ํด ์คํ๊ธฐ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ ์ด๋ค ๋ฉ์๋๋ฅผ ํธ์ถํด์ผ ํฉ๋๊น? newSingleThreadExecutor() ๋๋ newFixedThreadPool(1) ?
๋ ์คํ๊ธฐ ๋ชจ๋ ๋์ผํ ๋์์ ํฉ๋๋ค. ์ ์ผํ ์ฐจ์ด์ ์ newSingleThreadExecutor() ๋ฉ์๋๊ฐ ๋์ค์ ์ถ๊ฐ ์ค๋ ๋๋ฅผ ์ฌ์ฉํ๋๋ก ์ฌ๊ตฌ์ฑํ ์ ์๋ ์คํ๊ธฐ๋ฅผ ๋ฐํํ๋ค๋ ๊ฒ์ ๋๋ค.
GO TO FULL VERSION