async(๋น„๋™๊ธฐ) ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ThreadPoolTaskExecutor

ThreadPoolTaskExecutor๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ThreadPoolTaskExecutor๋Š” ์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋กœ org.springframework.scheduling.concurrent ํŒจํ‚ค์ง€์— ์†ํ•ด์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์„ฑ์ž๋„ ๊ธฐ๋ณธ์ƒ์„ฑ์ž ํ•˜๋‚˜๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ์“ฐ๋ ˆ๋“œํ’€์„ ์ด์šฉํ•˜์—ฌ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„์„ ์‰ฝ๊ฒŒ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

	public static void main(String[] args) {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.initialize();
	}

ThreadPoolTaskExecutor ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก initialize()๋ฅผ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด ์ด๋‹ˆ์…œ๋ผ์ด์ฆˆํ•˜๊ธฐ ์ „์—๋Š” executor๋ฅผ ์‚ฌ์šฉ์„ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋‹ˆ์…œ๋ผ์ด์ฆˆ ํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

Exception in thread "main" java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized

 

๊ทธ๋Ÿผ ์ด์ œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ์ถ”๊ฐ€ํ•œ ๋’ค ์‹ค์ œ๋กœ ์“ฐ๋ ˆ๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

	public static void main(String[] args) {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.initialize();
		
		log.info("executing threads....");
		Runnable r = () -> {
			try {
				log.info(Thread.currentThread().getName() + ", Now sleeping 10 seconds...");
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		};

		for (int i = 0; i < 10; i++) {
			executor.execute(r);
		}
	}

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”?

07:42:09.450 [main] INFO com.keichee.test.service.TestService - executing threads....
07:42:09.460 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:19.464 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:29.465 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:39.470 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:49.472 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:59.477 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:09.483 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:19.489 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:29.491 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:39.496 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...

๋กœ๊ทธ๊ฐ€ ์ถœ๋ ฅ๋œ ์‹œ๊ฐ„์„ ๋ณด๋ฉด 10์ดˆ๋งˆ๋‹ค ์ถœ๋ ฅ์ด ๋˜๊ณ ์žˆ๊ณ  ์“ฐ๋ ˆ๋“œ๋„ ThreadPoolTaskExecutor-1 ํ•˜๋‚˜๊ฐ€ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ง€๊ธˆ ์œ„ ์ฝ”๋“œ๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋กœ ๋Œ์•„๊ฐ„๊ฒŒ ์•„๋‹ˆ๋ž€ ์–˜๊ธฐ์ฃ . ThreadPoolTaskExecutor๋Š” ๋ช‡ ๊ฐ€์ง€ ์„ค์ •๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ค‘ corePoolSize ๊ฐ’์ด ๋™์‹œ์— ์‹คํ–‰ํ•  ์“ฐ๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ ์ด ์„ค์ •์˜ default ๊ฐ’์ด 1๋กœ ์„ธํŒ…๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ์ฒ˜๋Ÿผ corePoolSize ์„ค์ •์—†์ด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์‹ฑ๊ธ€์“ฐ๋ ˆ๋“œ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

์„ค์ •๊ฐ’

๊ทธ๋Ÿผ ์„ค์ •๊ฐ’๋“ค์— ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ๊ทธ๊ฒƒ๋“ค์ด ์˜๋ฏธํ•˜๋Š”๊ฒŒ ๋ฌด์—‡์ธ์ง€ setter ๋ฉ”์„œ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค‘์š”ํ•œ ๊ฐ’๋“ค๋งŒ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฉ”์„œ๋“œ

์„ค๋ช…

๊ธฐ๋ณธ๊ฐ’

setCorePoolSize

corePoolSize ๊ฐ’์„ ์„ค์ •ํ•จ. corePoolSize๋Š” ๋™์‹œ์— ์‹คํ–‰์‹œํ‚ฌ ์“ฐ๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•จ

1

setAllowCoreThreadTimeOut

์ฝ”์–ด ์“ฐ๋ ˆ๋“œ์˜ ํƒ€์ž„์•„์›ƒ์„ ํ—ˆ์šฉํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์„ธํ„ฐ ๋ฉ”์„œ๋“œ. true๋กœ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์ฝ”์–ด ์“ฐ๋ ˆ๋“œ๋ฅผ 10์œผ๋กœ ์„ค์ •ํ–ˆ์–ด๋„ ์ผ์ • ์‹œ๊ฐ„(keepAliveSeconds)์ด ์ง€๋‚˜๋ฉด ์ฝ”์–ด ์“ฐ๋ ˆ๋“œ ๊ฐœ์ˆ˜๊ฐ€ ์ค„์–ด๋“ฆ.

false

setKeepAliveSeconds

์ฝ”์–ด ์“ฐ๋ ˆ๋“œ ํƒ€์ž„์•„์›ƒ์„ ํ—ˆ์šฉํ–ˆ์„ ๊ฒฝ์šฐ ์‚ฌ์šฉ๋˜๋Š” ์„ค์ •๊ฐ’์œผ๋กœ, ์—ฌ๊ธฐ ์„ค์ •๋œ ์‹œ๊ฐ„์ด ์ง€๋‚  ๋•Œ๊นŒ์ง€ ์ฝ”์–ด ์“ฐ๋ ˆ๋“œ ํ’€์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ํ•ด๋‹น ์“ฐ๋ ˆ๋“œ๋Š” terminate ๋œ๋‹ค.

60์ดˆ

setMaxPoolSize

์“ฐ๋ ˆ๋“œ ํ’€์˜ ์ตœ๋Œ€ ์‚ฌ์ด์ฆˆ

Integer.MAX

setQueueCapacity

์“ฐ๋ ˆ๋“œ ํ’€ ํ์˜ ์‚ฌ์ด์ฆˆ. corePoolSize ๊ฐœ์ˆ˜๋ฅผ ๋„˜์–ด์„œ๋Š” task๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ queue์— ํ•ด๋‹น task๋“ค์ด ์Œ“์ด๊ฒŒ ๋œ๋‹ค. ์ตœ๋Œ€๋กœ maxPoolSize ๊ฐœ์ˆ˜ ๋งŒํผ ์Œ“์ผ ์ˆ˜ ์žˆ๋‹ค.

Integer.MAX

 

์—ฌ๊ธฐ์„œ corePoolSize, maxPoolSize, queueCapacity ์ด ์„ธ ๊ฐ€์ง€ ์„ค์ •๊ฐ’์ด ๊ฐ€์žฅ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

์šฐ์„  ์œ„์—์„œ ๋ดค๋˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” ์ด ์„ธ ๊ฐ€์ง€ ๊ฐ’์— ๋Œ€ํ•ด์„œ ๋ณ„๋„๋กœ ์„ค์ •์„ ํ•˜์ง€ ์•Š์•˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‹ฑ๊ธ€ ์“ฐ๋ ˆ๋“œ๋กœ ์ž‘์—…์ด ์ด๋ฃจ์–ด์กŒ์ฃ .

 

corePoolSize

์ด๋ฒˆ์—๋Š” corePoolSize๋ฅผ ์˜ฌ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.initialize();

        log.info("executing threads....");
        Runnable r = () -> {
            try {
                log.info(Thread.currentThread().getName() + ", Now sleeping 10 seconds...");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            executor.execute(r);
        }
    }

 

corePoolSize๋ฅผ 5๋กœ ์„ค์ • ํ›„ ์‹คํ–‰ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. (queueCapacity์™€ maxPoolSize๊ฐ’์€ ํ˜„์žฌ ๊ธฐ๋ณธ๊ฐ’์ธ Integer.MAX ์ž…๋‹ˆ๋‹ค)

08:52:50.423 [main] INFO com.keichee.test.service.TestService - executing threads....
08:52:50.456 [ThreadPoolTaskExecutor-3] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-3, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-2] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-2, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-5] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-5, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-4] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-4, Now sleeping 10 seconds...
08:53:00.460 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
08:53:00.460 [ThreadPoolTaskExecutor-2] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-2, Now sleeping 10 seconds...
08:53:00.461 [ThreadPoolTaskExecutor-3] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-3, Now sleeping 10 seconds...
08:53:00.461 [ThreadPoolTaskExecutor-4] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-4, Now sleeping 10 seconds...
08:53:00.461 [ThreadPoolTaskExecutor-5] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-5, Now sleeping 10 seconds...

์‹คํ–‰๋˜์ž๋งˆ์ž 5๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  10์ดˆ ํ›„์— ๋˜ ๋‹ค๋ฅธ 5๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, queueCapacity์™€ maxPoolSize๊ฐ’์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ํ•ด๋†“์œผ๋ฉด corePoolSize์˜ ๊ฐ’๋งŒํผ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

corePoolSize์™€ queueCapacity

๊ทธ๋Ÿผ ์ด๋ฒˆ์—๋Š” corePoolSize๋Š” default ๊ฐ’์ธ 1๋กœ ๋†”๋‘๊ณ  queueCapacity์™€ maxPoolSize ๊ฐ’์„ 5๋กœ ์„ค์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  10๊ฐœ์˜ task๊ฐ€ ์‹คํ–‰๋  ๋•Œ poolSize, activeSize, queueSize ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ถœ๋ ฅํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜, ์ถœ๋ ฅ์„ ์ข€ ์งง๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด์„œ ์“ฐ๋ ˆ๋“œ๋ช… prefix๋ฅผ "my-"๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("my-");
        executor.setQueueCapacity(5);
        executor.setMaxPoolSize(5);
        executor.initialize();

        log.info("executing threads....");
        Runnable r = () -> {
            try {
                log.info(Thread.currentThread().getName() + ", Now sleeping 10 seconds...");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            executor.execute(r);
            System.out.print("poolSize:" + executor.getPoolSize());
            System.out.print(", active:" + executor.getActiveCount());
            System.out.println(", queue:" + executor.getThreadPoolExecutor().getQueue().size());
        }
    }

 

์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™์€์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐ์„ ํ•ด๋ณด๊ณ  ์•„๋ž˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

09:02:22.864 [main] INFO com.keichee.test.service.TestService - executing threads....
poolSize:1, active:1, queue:0
poolSize:1, active:1, queue:1
poolSize:1, active:1, queue:2
poolSize:1, active:1, queue:3
poolSize:1, active:1, queue:4
poolSize:1, active:1, queue:5
poolSize:2, active:2, queue:5
09:02:22.886 [my-1] INFO com.keichee.test.service.TestService - my-1, Now sleeping 10 seconds...
09:02:22.887 [my-2] INFO com.keichee.test.service.TestService - my-2, Now sleeping 10 seconds...
poolSize:3, active:3, queue:5
09:02:22.887 [my-3] INFO com.keichee.test.service.TestService - my-3, Now sleeping 10 seconds...
poolSize:4, active:4, queue:5
09:02:22.887 [my-4] INFO com.keichee.test.service.TestService - my-4, Now sleeping 10 seconds...
poolSize:5, active:5, queue:5
09:02:22.887 [my-5] INFO com.keichee.test.service.TestService - my-5, Now sleeping 10 seconds...
09:02:32.888 [my-1] INFO com.keichee.test.service.TestService - my-1, Now sleeping 10 seconds...
09:02:32.890 [my-2] INFO com.keichee.test.service.TestService - my-2, Now sleeping 10 seconds...
09:02:32.890 [my-4] INFO com.keichee.test.service.TestService - my-4, Now sleeping 10 seconds...
09:02:32.890 [my-5] INFO com.keichee.test.service.TestService - my-5, Now sleeping 10 seconds...
09:02:32.890 [my-3] INFO com.keichee.test.service.TestService - my-3, Now sleeping 10 seconds...

 

 

์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด 10๊ฐœ์˜ task๋ฅผ ์‹คํ–‰ํ•  ๋•Œ queue ์‚ฌ์ด์ฆˆ๊ฐ€ 0์—์„œ 5๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๊ณ  ๊ทธ ์ดํ›„์— poolSize์™€ active ์‚ฌ์ด์ฆˆ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. corePoolSize๊ฐ€ 1 ์ด๋ผ์„œ 2๋ฒˆ์งธ task๋ถ€ํ„ฐ 6๋ฒˆ์งธ task๊นŒ์ง€๋Š” queue์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  7๋ฒˆ์งธ task๋ถ€ํ„ฐ 10๋ฒˆ์งธ task๊นŒ์ง€ 4๊ฐœ์˜ task๋Š” maxPoolSize ๋ฅผ ๋„˜์–ด์„œ์ง€ ์•Š๋Š” ํ•œ ์ถ”๊ฐ€๋กœ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ pool์— ๋„ฃ๊ณ  ํ•ด๋‹น ์“ฐ๋ ˆ๋“œ๋กœ ๊ฐ task๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ ๋งŒ์•ฝ maxPoolSize๋ฅผ ๋„˜์–ด์„ค ๋งŒํผ ๋งŽ์€ ์–‘์˜ task๋“ค์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

Exception in thread "main" org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@32eff876[Running, pool size = 5, active threads = 5, queued tasks = 5, completed tasks = 0]] did not accept task: com.keichee.test.service.TestService$$Lambda$22/0x00000008000ce840@5e0826e7
	at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:324)

TaskRejectedException ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

๋”ฐ๋ผ์„œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์–‘์˜ task๋ฅผ ์†Œํ™”ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ •ํ™•ํžˆ ์•Œ๊ณ  corePoolSize, queueCapacity, maxPoolSize์— ์ ์ ˆํ•œ ๊ฐ’์„ ์„ธํŒ…ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด TaskRejectedException์„ ๋ณผ ์ผ์€ ๊ฑฐ์˜ ์—†๊ฒ ์ง€๋งŒ ๊ทธ ๋Œ€์‹  queue์— ์–ด๋งˆ์–ด๋งˆํ•œ ์–‘์˜ task๊ฐ€ ์Œ“์ด๊ฒ ์ฃ . ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฐฐํฌ๋‚˜ ์–ด๋–ค ์ด์œ ์— ์˜ํ•ด์„œ ์žฌ๊ธฐ๋™์ด ํ•„์š”ํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น queue์— ์Œ“์—ฌ์žˆ๋˜ task๋“ค์€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ์‚ฌ์šฉํ•˜์‹ค ๋ถ„๋“ค์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ThreadPoolTaskExecutor๋ฅผ ์„ค์ • ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•