쓰레드풀 (2)

§ 쓰레드 기본 내용에 대해서는 [Java] 멀티쓰레드를 참고하세요.

 

ThreadPoolExecutor 를 이용하여 멀티쓰레드 구현하기

java.util.concurrent 패키지는 동시성 프로그래밍 관련하여 유용한 기능들을 모아둔 패키지입니다.

여기에는 ThreadPoolExecutor가 있는데 이것을 이용하면 손쉽게 멀티쓰레드를 구현할 수 있습니다.

우선 생성자에 어떤 것들이 있는지 확인해보겠습니다.

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)


기본적으로 corePoolSize, maximumPoolSize, keepAliveTime, timeUnit 그리고 workQueue를 입력받고 있네요.

각 항목이 의미하는 바가 뭔지 알아보겠습니다. java doc에는 아래와 같이 설명이 나와있습니다.

* @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set

* @param maximumPoolSize the maximum number of threads to allow in the pool

* @param keepAliveTime when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

* @param unit the time unit for the {@code keepAliveTime} argument

* @param workQueue the queue to use for holding tasks before they are executed.


간단히 말하면 corePoolSize는 쓰레드 풀의 기본 사이즈로 몇 개의 쓰레드를 pool에 가지고 있을지에 대한 값이고, maximumPoolSize는 쓰레드 풀의 max size로 이 사이즈를 넘어가면 RejectedExecutionException이 발생하게 됩니다. keepAliveTime은 idle 쓰레드의 keep alive time이며, timeUnit은 keepAliveTime의 시간단위를, 그리고 workQueue는 corePoolSize를 넘어서는 쓰레드들을 queueing 처리하기 위해 사용됩니다.

 

그럼 이제 간단한 예제 코드를 한번 보도록 하겠습니다.

    public static void main(String[] args) throws InterruptedException {

        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 100, 10, TimeUnit.SECONDS, blockingQueue);

        Runnable task = new Task();
        for (int i = 0; i < 100; i++) {
            threadPoolExecutor.execute(task);
        }
        System.out.println("쓰레드 콜 종료");
    }
    
    static class Task implements Runnable {

        int num = 0;

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ", num=" + num++);
            }
        }
    }

 

위 코드에서 ThreadPoolExecutor의 corePoolSize와 maximumPoolSize를 각각 100, 100 으로 설정을 해주었습니다.

위 코드를 실행하면 100개의 쓰레드가 각각 task를 실행하면서 아래와 같이 출력을 하게 됩니다. (더보기 클릭해서 보세요)

더보기

pool-1-thread-3, num=1
pool-1-thread-3, num=4
pool-1-thread-4, num=3
pool-1-thread-2, num=0
pool-1-thread-2, num=8
pool-1-thread-2, num=9
pool-1-thread-1, num=2
pool-1-thread-6, num=11
pool-1-thread-6, num=13
pool-1-thread-6, num=14
pool-1-thread-7, num=15
pool-1-thread-7, num=17
pool-1-thread-7, num=18
pool-1-thread-7, num=19
pool-1-thread-7, num=20
pool-1-thread-7, num=21
pool-1-thread-2, num=10
pool-1-thread-2, num=24
pool-1-thread-2, num=25
pool-1-thread-2, num=26
pool-1-thread-2, num=27
pool-1-thread-2, num=28
pool-1-thread-2, num=29
pool-1-thread-4, num=7
pool-1-thread-4, num=31
pool-1-thread-4, num=32
pool-1-thread-4, num=33
pool-1-thread-4, num=34
pool-1-thread-4, num=35
pool-1-thread-4, num=36
pool-1-thread-4, num=37
pool-1-thread-4, num=38
pool-1-thread-3, num=6
pool-1-thread-3, num=39
pool-1-thread-3, num=40
pool-1-thread-3, num=41
pool-1-thread-3, num=42
pool-1-thread-3, num=43
pool-1-thread-3, num=44
pool-1-thread-3, num=45
pool-1-thread-10, num=46
pool-1-thread-10, num=47
pool-1-thread-5, num=5
pool-1-thread-5, num=49
pool-1-thread-11, num=50
pool-1-thread-11, num=52
pool-1-thread-11, num=53
pool-1-thread-11, num=54
pool-1-thread-11, num=56
pool-1-thread-11, num=57
pool-1-thread-11, num=58
pool-1-thread-11, num=59
pool-1-thread-11, num=60
pool-1-thread-11, num=61
pool-1-thread-13, num=62
pool-1-thread-13, num=63
pool-1-thread-13, num=64
pool-1-thread-14, num=65
pool-1-thread-14, num=67
pool-1-thread-10, num=48
pool-1-thread-15, num=69
pool-1-thread-15, num=71
pool-1-thread-15, num=72
pool-1-thread-15, num=73
pool-1-thread-15, num=74
pool-1-thread-15, num=75
pool-1-thread-15, num=76
pool-1-thread-9, num=30
pool-1-thread-9, num=79
pool-1-thread-9, num=80
pool-1-thread-9, num=81
pool-1-thread-9, num=82
pool-1-thread-9, num=83
pool-1-thread-9, num=84
pool-1-thread-9, num=85
pool-1-thread-9, num=86
pool-1-thread-9, num=87
pool-1-thread-7, num=23
pool-1-thread-7, num=89
pool-1-thread-7, num=90
pool-1-thread-7, num=91
pool-1-thread-8, num=22
pool-1-thread-8, num=92
pool-1-thread-8, num=93
pool-1-thread-8, num=94
pool-1-thread-8, num=96
pool-1-thread-8, num=97
pool-1-thread-8, num=98
pool-1-thread-8, num=99
pool-1-thread-8, num=100
pool-1-thread-6, num=16
pool-1-thread-1, num=12
pool-1-thread-1, num=104
pool-1-thread-1, num=105
pool-1-thread-1, num=106
pool-1-thread-1, num=108
pool-1-thread-1, num=109
pool-1-thread-1, num=110
pool-1-thread-1, num=111
pool-1-thread-6, num=103
pool-1-thread-6, num=113
pool-1-thread-6, num=114
pool-1-thread-6, num=115
pool-1-thread-6, num=116
pool-1-thread-6, num=117
pool-1-thread-8, num=102
pool-1-thread-19, num=101
pool-1-thread-19, num=120
pool-1-thread-18, num=95
pool-1-thread-18, num=123
pool-1-thread-18, num=124
pool-1-thread-18, num=125
pool-1-thread-18, num=126
pool-1-thread-18, num=127
pool-1-thread-18, num=128
pool-1-thread-18, num=129
pool-1-thread-18, num=130
pool-1-thread-18, num=131
pool-1-thread-17, num=88
pool-1-thread-25, num=133
pool-1-thread-25, num=135
pool-1-thread-25, num=136
pool-1-thread-25, num=137
pool-1-thread-26, num=138
pool-1-thread-26, num=140
pool-1-thread-26, num=141
pool-1-thread-15, num=78
pool-1-thread-15, num=143
pool-1-thread-15, num=144
pool-1-thread-27, num=145
pool-1-thread-27, num=146
pool-1-thread-16, num=77
pool-1-thread-10, num=70
pool-1-thread-10, num=150
pool-1-thread-10, num=151
pool-1-thread-10, num=152
pool-1-thread-10, num=154
pool-1-thread-10, num=155
pool-1-thread-10, num=156
pool-1-thread-14, num=68
pool-1-thread-14, num=157
pool-1-thread-14, num=159
pool-1-thread-14, num=160
pool-1-thread-14, num=161
pool-1-thread-14, num=162
pool-1-thread-13, num=66
pool-1-thread-13, num=164
pool-1-thread-13, num=165
pool-1-thread-32, num=166
pool-1-thread-12, num=55
pool-1-thread-33, num=169
pool-1-thread-5, num=51
pool-1-thread-5, num=172
pool-1-thread-5, num=174
pool-1-thread-5, num=175
pool-1-thread-5, num=176
pool-1-thread-5, num=177
pool-1-thread-5, num=179
pool-1-thread-5, num=180
pool-1-thread-33, num=171
pool-1-thread-33, num=181
pool-1-thread-33, num=182
pool-1-thread-33, num=183
pool-1-thread-12, num=170
pool-1-thread-12, num=185
pool-1-thread-12, num=186
pool-1-thread-12, num=188
pool-1-thread-12, num=189
pool-1-thread-12, num=190
pool-1-thread-12, num=191
pool-1-thread-12, num=192
pool-1-thread-12, num=194
pool-1-thread-38, num=195
pool-1-thread-32, num=168
pool-1-thread-32, num=197
pool-1-thread-32, num=198
pool-1-thread-32, num=199
pool-1-thread-32, num=201
pool-1-thread-32, num=202
pool-1-thread-32, num=203
pool-1-thread-13, num=167
pool-1-thread-40, num=205
pool-1-thread-40, num=207
pool-1-thread-40, num=208
pool-1-thread-40, num=209
pool-1-thread-40, num=210
pool-1-thread-40, num=212
pool-1-thread-40, num=213
pool-1-thread-40, num=214
pool-1-thread-14, num=163
pool-1-thread-14, num=217
pool-1-thread-43, num=218
pool-1-thread-43, num=219
pool-1-thread-43, num=220
pool-1-thread-43, num=221
pool-1-thread-43, num=222
pool-1-thread-31, num=162
pool-1-thread-30, num=158
pool-1-thread-30, num=226
pool-1-thread-30, num=227
pool-1-thread-30, num=228
pool-1-thread-30, num=229
pool-1-thread-45, num=230
pool-1-thread-45, num=232
pool-1-thread-45, num=233
pool-1-thread-45, num=234
pool-1-thread-45, num=235
pool-1-thread-45, num=236
pool-1-thread-45, num=237
pool-1-thread-45, num=239
pool-1-thread-45, num=240
pool-1-thread-29, num=153
pool-1-thread-29, num=242
pool-1-thread-29, num=243
pool-1-thread-29, num=244
pool-1-thread-29, num=245
pool-1-thread-29, num=247
pool-1-thread-29, num=248
pool-1-thread-29, num=249
pool-1-thread-29, num=250
pool-1-thread-29, num=251
pool-1-thread-16, num=149
pool-1-thread-16, num=252
pool-1-thread-16, num=253
pool-1-thread-16, num=254
pool-1-thread-16, num=255
pool-1-thread-16, num=256
pool-1-thread-16, num=257
pool-1-thread-16, num=258
pool-1-thread-16, num=259
pool-1-thread-48, num=260
pool-1-thread-27, num=148
pool-1-thread-27, num=262
pool-1-thread-27, num=263
pool-1-thread-27, num=264
pool-1-thread-27, num=265
pool-1-thread-27, num=266
pool-1-thread-27, num=267
pool-1-thread-27, num=268
pool-1-thread-28, num=147
pool-1-thread-28, num=269
pool-1-thread-28, num=270
pool-1-thread-28, num=271
pool-1-thread-28, num=272
pool-1-thread-28, num=273
pool-1-thread-28, num=274
pool-1-thread-28, num=275
pool-1-thread-28, num=276
pool-1-thread-28, num=278
pool-1-thread-26, num=142
pool-1-thread-26, num=279
pool-1-thread-26, num=280
pool-1-thread-26, num=281
pool-1-thread-26, num=282
pool-1-thread-25, num=139
pool-1-thread-25, num=284
pool-1-thread-25, num=286
pool-1-thread-25, num=287
pool-1-thread-25, num=288
pool-1-thread-25, num=290
pool-1-thread-17, num=134
pool-1-thread-52, num=291
pool-1-thread-52, num=293
pool-1-thread-24, num=132
pool-1-thread-53, num=295
pool-1-thread-53, num=297
pool-1-thread-53, num=298
pool-1-thread-53, num=299
pool-1-thread-53, num=300
pool-1-thread-53, num=302
pool-1-thread-53, num=303
pool-1-thread-53, num=304
pool-1-thread-53, num=305
pool-1-thread-53, num=306
pool-1-thread-55, num=307
pool-1-thread-55, num=308
pool-1-thread-55, num=309
pool-1-thread-55, num=310
pool-1-thread-55, num=311
pool-1-thread-55, num=312
pool-1-thread-55, num=313
pool-1-thread-55, num=314
pool-1-thread-55, num=315
pool-1-thread-55, num=316
pool-1-thread-23, num=122
pool-1-thread-56, num=317
pool-1-thread-56, num=318
pool-1-thread-56, num=319
pool-1-thread-56, num=320
pool-1-thread-56, num=321
pool-1-thread-23, num=322
pool-1-thread-23, num=324
pool-1-thread-23, num=325
pool-1-thread-23, num=326
pool-1-thread-19, num=121
pool-1-thread-19, num=328
pool-1-thread-19, num=329
pool-1-thread-19, num=330
pool-1-thread-19, num=332
pool-1-thread-19, num=333
pool-1-thread-19, num=334
pool-1-thread-19, num=335
pool-1-thread-22, num=119
pool-1-thread-22, num=336
pool-1-thread-22, num=337
pool-1-thread-21, num=118
pool-1-thread-21, num=340
pool-1-thread-21, num=341
pool-1-thread-21, num=342
pool-1-thread-21, num=343
pool-1-thread-1, num=112
pool-1-thread-59, num=345
pool-1-thread-20, num=107
pool-1-thread-59, num=346
pool-1-thread-59, num=348
pool-1-thread-59, num=349
pool-1-thread-59, num=350
pool-1-thread-21, num=344
pool-1-thread-21, num=353
pool-1-thread-21, num=354
pool-1-thread-21, num=355
pool-1-thread-21, num=356
pool-1-thread-22, num=339
pool-1-thread-22, num=358
pool-1-thread-58, num=338
pool-1-thread-58, num=360
pool-1-thread-58, num=361
pool-1-thread-58, num=362
pool-1-thread-58, num=363
pool-1-thread-58, num=364
pool-1-thread-62, num=365
pool-1-thread-57, num=331
pool-1-thread-57, num=368
pool-1-thread-57, num=369
pool-1-thread-57, num=371
pool-1-thread-57, num=372
pool-1-thread-57, num=373
pool-1-thread-57, num=375
pool-1-thread-57, num=376
pool-1-thread-57, num=377
pool-1-thread-57, num=378
pool-1-thread-23, num=327
pool-1-thread-65, num=379
pool-1-thread-65, num=380
pool-1-thread-65, num=382
pool-1-thread-65, num=383
pool-1-thread-65, num=384
pool-1-thread-56, num=323
pool-1-thread-56, num=387
pool-1-thread-56, num=388
pool-1-thread-56, num=389
pool-1-thread-56, num=390
pool-1-thread-54, num=301
pool-1-thread-54, num=391
pool-1-thread-54, num=392
pool-1-thread-54, num=394
pool-1-thread-54, num=395
pool-1-thread-54, num=396
pool-1-thread-54, num=397
pool-1-thread-54, num=398
pool-1-thread-54, num=399
pool-1-thread-54, num=401
pool-1-thread-24, num=296
pool-1-thread-24, num=403
pool-1-thread-52, num=294
pool-1-thread-52, num=406
pool-1-thread-52, num=407
pool-1-thread-52, num=408
pool-1-thread-52, num=410
pool-1-thread-52, num=411
pool-1-thread-52, num=412
pool-1-thread-52, num=413
pool-1-thread-17, num=292
pool-1-thread-17, num=415
pool-1-thread-17, num=416
pool-1-thread-17, num=418
pool-1-thread-74, num=419
pool-1-thread-50, num=289
pool-1-thread-50, num=422
pool-1-thread-75, num=423
pool-1-thread-75, num=425
pool-1-thread-75, num=426
pool-1-thread-51, num=285
pool-1-thread-51, num=428
pool-1-thread-51, num=431
pool-1-thread-51, num=432
pool-1-thread-77, num=430
pool-1-thread-77, num=434
pool-1-thread-77, num=435
pool-1-thread-77, num=436
pool-1-thread-77, num=437
pool-1-thread-77, num=439
pool-1-thread-77, num=440
pool-1-thread-26, num=283
pool-1-thread-79, num=442
pool-1-thread-79, num=444
pool-1-thread-49, num=277
pool-1-thread-49, num=446
pool-1-thread-49, num=447
pool-1-thread-49, num=448
pool-1-thread-49, num=449
pool-1-thread-49, num=450
pool-1-thread-49, num=451
pool-1-thread-49, num=452
pool-1-thread-48, num=261
pool-1-thread-47, num=246
pool-1-thread-47, num=456
pool-1-thread-47, num=457
pool-1-thread-47, num=459
pool-1-thread-82, num=460
pool-1-thread-45, num=241
pool-1-thread-83, num=463
pool-1-thread-83, num=464
pool-1-thread-46, num=238
pool-1-thread-83, num=465
pool-1-thread-83, num=466
pool-1-thread-83, num=467
pool-1-thread-83, num=469
pool-1-thread-83, num=471
pool-1-thread-83, num=472
pool-1-thread-83, num=473
pool-1-thread-83, num=474
pool-1-thread-30, num=231
pool-1-thread-30, num=475
pool-1-thread-30, num=476
pool-1-thread-30, num=477
pool-1-thread-30, num=478
pool-1-thread-31, num=225
pool-1-thread-43, num=224
pool-1-thread-43, num=481
pool-1-thread-43, num=482
pool-1-thread-43, num=483
pool-1-thread-43, num=484
pool-1-thread-86, num=485
pool-1-thread-86, num=486
pool-1-thread-86, num=487
pool-1-thread-86, num=488
pool-1-thread-86, num=489
pool-1-thread-86, num=490
pool-1-thread-86, num=491
pool-1-thread-86, num=492
pool-1-thread-86, num=493
pool-1-thread-86, num=494
pool-1-thread-44, num=223
pool-1-thread-44, num=495
pool-1-thread-44, num=496
pool-1-thread-44, num=497
pool-1-thread-44, num=498
pool-1-thread-44, num=499
pool-1-thread-44, num=500
pool-1-thread-44, num=501
pool-1-thread-44, num=502
pool-1-thread-44, num=503
pool-1-thread-42, num=216
pool-1-thread-42, num=504
pool-1-thread-42, num=505
pool-1-thread-42, num=506
pool-1-thread-42, num=507
pool-1-thread-42, num=508
pool-1-thread-42, num=509
pool-1-thread-42, num=510
pool-1-thread-42, num=511
pool-1-thread-42, num=512
pool-1-thread-40, num=215
pool-1-thread-40, num=514
pool-1-thread-41, num=211
pool-1-thread-41, num=515
pool-1-thread-41, num=516
pool-1-thread-41, num=517
pool-1-thread-41, num=519
pool-1-thread-41, num=520
pool-1-thread-41, num=521
pool-1-thread-41, num=522
pool-1-thread-41, num=523
pool-1-thread-41, num=524
pool-1-thread-13, num=206
pool-1-thread-13, num=525
pool-1-thread-13, num=526
pool-1-thread-32, num=204
pool-1-thread-32, num=527
pool-1-thread-89, num=528
pool-1-thread-89, num=529
pool-1-thread-89, num=530
pool-1-thread-89, num=531
pool-1-thread-89, num=532
pool-1-thread-89, num=533
pool-1-thread-89, num=534
pool-1-thread-89, num=535
pool-1-thread-89, num=536
pool-1-thread-89, num=537
pool-1-thread-39, num=200
pool-1-thread-39, num=538
pool-1-thread-39, num=539
pool-1-thread-39, num=540
pool-1-thread-39, num=541
pool-1-thread-39, num=542
pool-1-thread-39, num=543
pool-1-thread-39, num=544
pool-1-thread-39, num=545
pool-1-thread-39, num=546
pool-1-thread-38, num=196
pool-1-thread-38, num=547
pool-1-thread-38, num=548
pool-1-thread-38, num=549
pool-1-thread-38, num=550
pool-1-thread-38, num=551
pool-1-thread-38, num=552
pool-1-thread-90, num=553
pool-1-thread-90, num=555
pool-1-thread-90, num=556
pool-1-thread-90, num=557
pool-1-thread-90, num=558
pool-1-thread-90, num=559
pool-1-thread-90, num=560
pool-1-thread-90, num=561
pool-1-thread-90, num=562
pool-1-thread-90, num=563
pool-1-thread-37, num=193
pool-1-thread-37, num=564
pool-1-thread-37, num=565
pool-1-thread-37, num=566
pool-1-thread-37, num=567
pool-1-thread-37, num=568
pool-1-thread-37, num=569
pool-1-thread-37, num=570
pool-1-thread-37, num=571
pool-1-thread-37, num=572
pool-1-thread-36, num=187
pool-1-thread-36, num=573
pool-1-thread-36, num=574
pool-1-thread-36, num=575
pool-1-thread-36, num=576
pool-1-thread-36, num=577
pool-1-thread-36, num=578
pool-1-thread-36, num=579
pool-1-thread-36, num=580
pool-1-thread-36, num=581
pool-1-thread-91, num=582
pool-1-thread-91, num=583
pool-1-thread-91, num=584
pool-1-thread-91, num=585
pool-1-thread-91, num=586
pool-1-thread-91, num=587
pool-1-thread-91, num=588
pool-1-thread-91, num=589
pool-1-thread-91, num=590
pool-1-thread-91, num=591
pool-1-thread-33, num=184
pool-1-thread-33, num=592
pool-1-thread-33, num=593
pool-1-thread-33, num=594
pool-1-thread-33, num=595
pool-1-thread-35, num=178
pool-1-thread-35, num=596
pool-1-thread-35, num=597
pool-1-thread-35, num=598
pool-1-thread-35, num=599
pool-1-thread-35, num=600
pool-1-thread-35, num=601
pool-1-thread-35, num=602
pool-1-thread-35, num=603
pool-1-thread-35, num=604
pool-1-thread-92, num=605
pool-1-thread-92, num=606
pool-1-thread-92, num=607
pool-1-thread-92, num=608
pool-1-thread-92, num=609
pool-1-thread-92, num=610
pool-1-thread-92, num=611
pool-1-thread-92, num=612
pool-1-thread-92, num=613
pool-1-thread-92, num=614
pool-1-thread-34, num=173
pool-1-thread-34, num=615
pool-1-thread-34, num=616
pool-1-thread-34, num=617
pool-1-thread-34, num=618
pool-1-thread-34, num=619
pool-1-thread-34, num=620
pool-1-thread-34, num=621
pool-1-thread-34, num=622
pool-1-thread-34, num=623
pool-1-thread-38, num=554
pool-1-thread-38, num=624
pool-1-thread-88, num=518
pool-1-thread-88, num=625
pool-1-thread-88, num=626
pool-1-thread-88, num=627
pool-1-thread-88, num=628
pool-1-thread-88, num=629
pool-1-thread-88, num=630
pool-1-thread-88, num=631
pool-1-thread-88, num=632
pool-1-thread-88, num=633
pool-1-thread-93, num=634
pool-1-thread-93, num=635
pool-1-thread-93, num=636
pool-1-thread-93, num=637
pool-1-thread-93, num=638
pool-1-thread-93, num=639
pool-1-thread-93, num=640
pool-1-thread-93, num=641
pool-1-thread-93, num=642
pool-1-thread-93, num=643
pool-1-thread-87, num=513
pool-1-thread-87, num=644
pool-1-thread-87, num=645
pool-1-thread-87, num=646
pool-1-thread-87, num=647
pool-1-thread-87, num=648
pool-1-thread-87, num=649
pool-1-thread-87, num=650
pool-1-thread-87, num=651
pool-1-thread-87, num=652
pool-1-thread-31, num=480
pool-1-thread-31, num=653
pool-1-thread-31, num=654
pool-1-thread-31, num=655
pool-1-thread-31, num=656
pool-1-thread-31, num=657
pool-1-thread-31, num=658
pool-1-thread-31, num=659
pool-1-thread-94, num=660
pool-1-thread-94, num=661
pool-1-thread-94, num=662
pool-1-thread-94, num=663
pool-1-thread-94, num=664
pool-1-thread-94, num=665
pool-1-thread-94, num=666
pool-1-thread-94, num=667
pool-1-thread-94, num=668
pool-1-thread-94, num=669
pool-1-thread-85, num=479
pool-1-thread-85, num=670
pool-1-thread-85, num=671
pool-1-thread-85, num=672
pool-1-thread-85, num=673
pool-1-thread-85, num=674
pool-1-thread-95, num=675
pool-1-thread-95, num=676
pool-1-thread-95, num=677
pool-1-thread-95, num=678
pool-1-thread-95, num=679
pool-1-thread-95, num=680
pool-1-thread-95, num=681
pool-1-thread-95, num=682
pool-1-thread-95, num=683
pool-1-thread-95, num=684
pool-1-thread-46, num=470
pool-1-thread-46, num=686
pool-1-thread-46, num=687
pool-1-thread-46, num=688
pool-1-thread-46, num=689
pool-1-thread-46, num=690
pool-1-thread-46, num=691
pool-1-thread-46, num=692
pool-1-thread-46, num=693
pool-1-thread-84, num=468
pool-1-thread-84, num=694
pool-1-thread-84, num=695
pool-1-thread-84, num=696
pool-1-thread-84, num=697
pool-1-thread-84, num=698
pool-1-thread-84, num=699
pool-1-thread-84, num=700
pool-1-thread-82, num=462
pool-1-thread-82, num=703
pool-1-thread-47, num=461
pool-1-thread-47, num=705
pool-1-thread-47, num=706
pool-1-thread-47, num=707
pool-1-thread-47, num=708
pool-1-thread-47, num=709
pool-1-thread-81, num=458
pool-1-thread-81, num=710
pool-1-thread-81, num=712
pool-1-thread-81, num=713
pool-1-thread-81, num=714
pool-1-thread-81, num=715
pool-1-thread-81, num=716
pool-1-thread-48, num=455
pool-1-thread-49, num=454
pool-1-thread-49, num=720
pool-1-thread-80, num=453
pool-1-thread-80, num=721
pool-1-thread-80, num=722
pool-1-thread-80, num=723
pool-1-thread-80, num=724
pool-1-thread-80, num=725
pool-1-thread-80, num=726
pool-1-thread-80, num=727
pool-1-thread-80, num=728
pool-1-thread-79, num=445
pool-1-thread-79, num=731
pool-1-thread-26, num=443
pool-1-thread-77, num=441
pool-1-thread-77, num=733
pool-1-thread-77, num=734
pool-1-thread-78, num=438
pool-1-thread-78, num=735
pool-1-thread-78, num=737
pool-1-thread-78, num=738
pool-1-thread-51, num=433
pool-1-thread-51, num=740
pool-1-thread-51, num=741
pool-1-thread-51, num=742
pool-1-thread-51, num=743
pool-1-thread-51, num=744
pool-1-thread-76, num=429
pool-1-thread-76, num=745
pool-1-thread-76, num=746
pool-1-thread-76, num=747
pool-1-thread-75, num=427
pool-1-thread-75, num=749
pool-1-thread-75, num=750
pool-1-thread-75, num=751
pool-1-thread-75, num=752
pool-1-thread-75, num=753
pool-1-thread-50, num=424
pool-1-thread-74, num=421
pool-1-thread-17, num=420
pool-1-thread-17, num=757
pool-1-thread-17, num=758
pool-1-thread-17, num=759
pool-1-thread-73, num=417
pool-1-thread-73, num=760
pool-1-thread-73, num=761
pool-1-thread-73, num=762
pool-1-thread-73, num=763
pool-1-thread-73, num=764
pool-1-thread-73, num=765
pool-1-thread-73, num=766
pool-1-thread-73, num=767
pool-1-thread-73, num=768
pool-1-thread-72, num=414
pool-1-thread-72, num=769
pool-1-thread-72, num=770
pool-1-thread-72, num=771
pool-1-thread-72, num=772
pool-1-thread-72, num=773
pool-1-thread-72, num=774
pool-1-thread-72, num=775
pool-1-thread-72, num=776
pool-1-thread-72, num=777
pool-1-thread-71, num=409
pool-1-thread-71, num=778
pool-1-thread-71, num=779
pool-1-thread-71, num=780
pool-1-thread-24, num=405
pool-1-thread-24, num=782
pool-1-thread-24, num=783
pool-1-thread-24, num=784
pool-1-thread-24, num=785
pool-1-thread-24, num=786
pool-1-thread-24, num=787
pool-1-thread-70, num=404
pool-1-thread-70, num=788
pool-1-thread-70, num=789
pool-1-thread-70, num=790
pool-1-thread-70, num=791
pool-1-thread-70, num=792
pool-1-thread-70, num=793
pool-1-thread-70, num=794
pool-1-thread-70, num=795
pool-1-thread-69, num=402
pool-1-thread-69, num=797
pool-1-thread-68, num=400
pool-1-thread-68, num=799
pool-1-thread-68, num=800
pool-1-thread-68, num=801
pool-1-thread-68, num=802
pool-1-thread-68, num=803
pool-1-thread-68, num=804
pool-1-thread-67, num=393
pool-1-thread-67, num=806
pool-1-thread-67, num=807
pool-1-thread-67, num=808
pool-1-thread-65, num=386
pool-1-thread-67, num=809
pool-1-thread-67, num=810
pool-1-thread-67, num=811
pool-1-thread-67, num=812
pool-1-thread-67, num=813
pool-1-thread-67, num=814
pool-1-thread-65, num=815
pool-1-thread-65, num=816
pool-1-thread-65, num=817
pool-1-thread-65, num=818
pool-1-thread-66, num=385
pool-1-thread-23, num=381
pool-1-thread-66, num=819
pool-1-thread-66, num=820
pool-1-thread-66, num=821
pool-1-thread-66, num=822
pool-1-thread-66, num=823
pool-1-thread-66, num=824
pool-1-thread-66, num=825
pool-1-thread-66, num=826
pool-1-thread-66, num=827
pool-1-thread-23, num=828
pool-1-thread-23, num=829
pool-1-thread-23, num=830
pool-1-thread-64, num=374
pool-1-thread-63, num=370
pool-1-thread-64, num=831
pool-1-thread-64, num=832
pool-1-thread-64, num=833
pool-1-thread-64, num=834
pool-1-thread-64, num=835
pool-1-thread-64, num=836
pool-1-thread-64, num=837
pool-1-thread-64, num=838
pool-1-thread-64, num=839
pool-1-thread-63, num=840
pool-1-thread-63, num=841
pool-1-thread-63, num=842
pool-1-thread-63, num=843
pool-1-thread-63, num=844
pool-1-thread-63, num=845
pool-1-thread-63, num=846
pool-1-thread-63, num=847
pool-1-thread-63, num=848
pool-1-thread-62, num=367
pool-1-thread-58, num=366
pool-1-thread-62, num=849
pool-1-thread-62, num=850
pool-1-thread-62, num=851
pool-1-thread-62, num=852
pool-1-thread-62, num=853
pool-1-thread-62, num=854
pool-1-thread-62, num=855
pool-1-thread-62, num=856
pool-1-thread-58, num=857
pool-1-thread-58, num=858
pool-1-thread-58, num=859
pool-1-thread-22, num=359
pool-1-thread-61, num=357
pool-1-thread-22, num=860
pool-1-thread-22, num=861
pool-1-thread-22, num=862
pool-1-thread-22, num=863
pool-1-thread-61, num=864
pool-1-thread-61, num=865
pool-1-thread-61, num=866
pool-1-thread-61, num=867
pool-1-thread-61, num=868
pool-1-thread-61, num=869
pool-1-thread-61, num=870
pool-1-thread-61, num=871
pool-1-thread-61, num=872
pool-1-thread-59, num=352
pool-1-thread-60, num=351
pool-1-thread-59, num=873
pool-1-thread-59, num=874
pool-1-thread-59, num=875
pool-1-thread-59, num=876
pool-1-thread-60, num=877
pool-1-thread-60, num=878
pool-1-thread-60, num=879
pool-1-thread-60, num=880
pool-1-thread-60, num=881
pool-1-thread-60, num=882
pool-1-thread-60, num=883
pool-1-thread-60, num=884
pool-1-thread-60, num=885
pool-1-thread-20, num=347
pool-1-thread-20, num=886
pool-1-thread-20, num=887
pool-1-thread-20, num=888
pool-1-thread-20, num=889
pool-1-thread-20, num=890
pool-1-thread-20, num=891
pool-1-thread-20, num=892
pool-1-thread-20, num=893
pool-1-thread-68, num=805
pool-1-thread-69, num=798
pool-1-thread-68, num=894
pool-1-thread-68, num=895
pool-1-thread-69, num=896
pool-1-thread-69, num=897
pool-1-thread-69, num=898
pool-1-thread-69, num=899
pool-1-thread-70, num=796
pool-1-thread-71, num=781
pool-1-thread-71, num=901
pool-1-thread-71, num=902
pool-1-thread-71, num=903
pool-1-thread-71, num=904
pool-1-thread-71, num=905
pool-1-thread-74, num=756
pool-1-thread-74, num=906
pool-1-thread-74, num=907
pool-1-thread-74, num=908
pool-1-thread-74, num=909
pool-1-thread-74, num=910
pool-1-thread-74, num=911
pool-1-thread-74, num=912
pool-1-thread-50, num=755
pool-1-thread-50, num=913
pool-1-thread-50, num=914
pool-1-thread-50, num=915
pool-1-thread-50, num=916
pool-1-thread-50, num=917
pool-1-thread-50, num=918
pool-1-thread-75, num=754
pool-1-thread-76, num=748
pool-1-thread-76, num=919
pool-1-thread-76, num=920
pool-1-thread-76, num=921
pool-1-thread-76, num=922
pool-1-thread-76, num=923
pool-1-thread-78, num=739
pool-1-thread-100, num=736
pool-1-thread-100, num=925
pool-1-thread-100, num=926
pool-1-thread-100, num=927
pool-1-thread-100, num=928
pool-1-thread-100, num=929
pool-1-thread-100, num=930
pool-1-thread-100, num=931
pool-1-thread-100, num=932
pool-1-thread-100, num=933
pool-1-thread-79, num=732
쓰레드 콜 종료
pool-1-thread-79, num=934
pool-1-thread-79, num=935
pool-1-thread-79, num=936
pool-1-thread-79, num=937
pool-1-thread-79, num=938
pool-1-thread-80, num=730
pool-1-thread-99, num=729
pool-1-thread-99, num=939
pool-1-thread-99, num=940
pool-1-thread-99, num=941
pool-1-thread-99, num=942
pool-1-thread-99, num=943
pool-1-thread-99, num=944
pool-1-thread-99, num=945
pool-1-thread-99, num=946
pool-1-thread-99, num=947
pool-1-thread-48, num=719
pool-1-thread-48, num=948
pool-1-thread-48, num=949
pool-1-thread-48, num=950
pool-1-thread-48, num=951
pool-1-thread-48, num=952
pool-1-thread-48, num=953
pool-1-thread-81, num=717
pool-1-thread-81, num=954
pool-1-thread-81, num=955
pool-1-thread-98, num=718
pool-1-thread-98, num=956
pool-1-thread-98, num=957
pool-1-thread-98, num=958
pool-1-thread-98, num=959
pool-1-thread-98, num=960
pool-1-thread-98, num=961
pool-1-thread-98, num=962
pool-1-thread-98, num=963
pool-1-thread-98, num=964
pool-1-thread-97, num=711
pool-1-thread-97, num=965
pool-1-thread-97, num=966
pool-1-thread-97, num=967
pool-1-thread-97, num=968
pool-1-thread-97, num=969
pool-1-thread-97, num=970
pool-1-thread-97, num=971
pool-1-thread-97, num=972
pool-1-thread-97, num=973
pool-1-thread-82, num=704
pool-1-thread-82, num=974
pool-1-thread-82, num=975
pool-1-thread-82, num=976
pool-1-thread-82, num=977
pool-1-thread-82, num=978
pool-1-thread-82, num=979
pool-1-thread-84, num=702
pool-1-thread-84, num=980
pool-1-thread-96, num=701
pool-1-thread-96, num=981
pool-1-thread-96, num=982
pool-1-thread-96, num=983
pool-1-thread-96, num=984
pool-1-thread-96, num=985
pool-1-thread-96, num=986
pool-1-thread-96, num=987
pool-1-thread-96, num=988
pool-1-thread-96, num=989
pool-1-thread-85, num=685
pool-1-thread-85, num=990
pool-1-thread-85, num=991
pool-1-thread-85, num=992
pool-1-thread-78, num=924
pool-1-thread-78, num=993
pool-1-thread-78, num=994
pool-1-thread-78, num=995
pool-1-thread-78, num=996
pool-1-thread-69, num=900
pool-1-thread-69, num=997
pool-1-thread-69, num=998
executor has been terminated

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

 

위 출력 결과를 자세히 확인해보면 thread는 thread-1 ~ thread-100 까지 총 100개가 생성되어 실행이 된 것을 확인 할 수 있습니다. 

이번에는 corePoolSize를 1로 설정을 해서 실행해보겠습니다. (더보기 클릭해서 보세요)

더보기

pool-1-thread-2, num=0
pool-1-thread-3, num=0
pool-1-thread-1, num=1
pool-1-thread-3, num=3
pool-1-thread-3, num=5
pool-1-thread-2, num=2
pool-1-thread-2, num=7
pool-1-thread-3, num=6
pool-1-thread-1, num=4
pool-1-thread-3, num=9
pool-1-thread-2, num=8
pool-1-thread-3, num=11
pool-1-thread-1, num=10
pool-1-thread-3, num=13
pool-1-thread-2, num=12
pool-1-thread-3, num=15
pool-1-thread-1, num=14
pool-1-thread-1, num=18
pool-1-thread-3, num=17
pool-1-thread-2, num=16
pool-1-thread-3, num=20
pool-1-thread-1, num=19
pool-1-thread-2, num=21
pool-1-thread-3, num=23
pool-1-thread-1, num=22
pool-1-thread-1, num=26
pool-1-thread-3, num=25
pool-1-thread-2, num=24
pool-1-thread-2, num=29
pool-1-thread-2, num=30
pool-1-thread-3, num=28
pool-1-thread-1, num=27
pool-1-thread-3, num=31
pool-1-thread-3, num=33
pool-1-thread-3, num=34
pool-1-thread-3, num=35
pool-1-thread-3, num=36
pool-1-thread-1, num=32
pool-1-thread-3, num=37
pool-1-thread-3, num=38
pool-1-thread-4, num=39
pool-1-thread-4, num=40
pool-1-thread-4, num=41
pool-1-thread-4, num=42
pool-1-thread-4, num=43
pool-1-thread-4, num=44
pool-1-thread-4, num=45
pool-1-thread-4, num=46
pool-1-thread-4, num=47
pool-1-thread-4, num=48
pool-1-thread-4, num=49
pool-1-thread-4, num=50
pool-1-thread-2, num=51
pool-1-thread-4, num=52
pool-1-thread-4, num=54
pool-1-thread-2, num=53
pool-1-thread-4, num=55
pool-1-thread-4, num=57
pool-1-thread-4, num=58
pool-1-thread-2, num=56
pool-1-thread-4, num=59
pool-1-thread-2, num=60
pool-1-thread-4, num=61
pool-1-thread-2, num=62
pool-1-thread-2, num=64
pool-1-thread-4, num=63
pool-1-thread-2, num=65
pool-1-thread-2, num=66
pool-1-thread-2, num=67
pool-1-thread-2, num=68
pool-1-thread-5, num=69
pool-1-thread-5, num=70
pool-1-thread-5, num=71
pool-1-thread-5, num=72
pool-1-thread-5, num=73
pool-1-thread-5, num=74
pool-1-thread-5, num=75
pool-1-thread-5, num=76
pool-1-thread-5, num=77
pool-1-thread-5, num=78
pool-1-thread-5, num=79
pool-1-thread-5, num=80
pool-1-thread-5, num=81
pool-1-thread-5, num=82
pool-1-thread-5, num=83
pool-1-thread-3, num=84
pool-1-thread-5, num=85
pool-1-thread-3, num=86
pool-1-thread-5, num=87
pool-1-thread-3, num=88
pool-1-thread-3, num=90
pool-1-thread-5, num=89
pool-1-thread-3, num=91
pool-1-thread-5, num=92
pool-1-thread-3, num=93
pool-1-thread-5, num=94
pool-1-thread-3, num=95
pool-1-thread-3, num=96
pool-1-thread-3, num=97
pool-1-thread-3, num=98
pool-1-thread-6, num=99
pool-1-thread-6, num=100
pool-1-thread-6, num=101
pool-1-thread-6, num=102
pool-1-thread-6, num=103
pool-1-thread-6, num=104
pool-1-thread-6, num=105
pool-1-thread-6, num=106
pool-1-thread-6, num=107
pool-1-thread-6, num=108
pool-1-thread-4, num=109
pool-1-thread-4, num=110
pool-1-thread-4, num=111
pool-1-thread-4, num=112
pool-1-thread-4, num=113
pool-1-thread-4, num=114
pool-1-thread-4, num=115
pool-1-thread-4, num=116
pool-1-thread-4, num=117
pool-1-thread-4, num=118
pool-1-thread-2, num=119
pool-1-thread-2, num=120
pool-1-thread-2, num=121
pool-1-thread-2, num=122
pool-1-thread-2, num=123
pool-1-thread-2, num=124
pool-1-thread-2, num=125
pool-1-thread-2, num=126
pool-1-thread-2, num=127
pool-1-thread-2, num=128
pool-1-thread-1, num=129
pool-1-thread-1, num=130
pool-1-thread-1, num=131
pool-1-thread-1, num=132
pool-1-thread-1, num=133
pool-1-thread-1, num=134
pool-1-thread-1, num=135
pool-1-thread-1, num=136
pool-1-thread-1, num=137
pool-1-thread-1, num=138
pool-1-thread-7, num=139
pool-1-thread-7, num=140
pool-1-thread-7, num=141
pool-1-thread-7, num=142
pool-1-thread-7, num=143
pool-1-thread-7, num=144
pool-1-thread-7, num=145
pool-1-thread-7, num=146
pool-1-thread-7, num=147
pool-1-thread-7, num=148
pool-1-thread-5, num=149
pool-1-thread-5, num=150
pool-1-thread-5, num=151
pool-1-thread-5, num=152
pool-1-thread-5, num=153
pool-1-thread-5, num=154
pool-1-thread-5, num=155
pool-1-thread-5, num=156
pool-1-thread-5, num=157
pool-1-thread-5, num=158
pool-1-thread-3, num=159
pool-1-thread-3, num=160
pool-1-thread-3, num=161
pool-1-thread-3, num=162
pool-1-thread-3, num=163
pool-1-thread-3, num=164
pool-1-thread-3, num=165
pool-1-thread-3, num=166
pool-1-thread-3, num=167
pool-1-thread-3, num=168
pool-1-thread-8, num=169
pool-1-thread-8, num=170
pool-1-thread-8, num=171
pool-1-thread-8, num=172
pool-1-thread-8, num=173
pool-1-thread-8, num=174
pool-1-thread-8, num=175
pool-1-thread-8, num=176
pool-1-thread-8, num=177
pool-1-thread-8, num=178
pool-1-thread-6, num=179
pool-1-thread-6, num=180
pool-1-thread-6, num=181
pool-1-thread-6, num=182
pool-1-thread-6, num=183
pool-1-thread-6, num=184
pool-1-thread-6, num=185
pool-1-thread-6, num=186
pool-1-thread-6, num=187
pool-1-thread-6, num=188
pool-1-thread-4, num=189
pool-1-thread-9, num=190
pool-1-thread-9, num=192
pool-1-thread-9, num=193
pool-1-thread-9, num=194
pool-1-thread-4, num=191
pool-1-thread-9, num=195
pool-1-thread-4, num=196
pool-1-thread-9, num=197
pool-1-thread-4, num=198
pool-1-thread-4, num=200
pool-1-thread-9, num=199
pool-1-thread-9, num=202
pool-1-thread-4, num=201
pool-1-thread-9, num=203
pool-1-thread-9, num=205
pool-1-thread-4, num=204
pool-1-thread-4, num=206
pool-1-thread-4, num=207
pool-1-thread-4, num=208
pool-1-thread-2, num=209
pool-1-thread-2, num=210
pool-1-thread-10, num=211
pool-1-thread-10, num=213
pool-1-thread-10, num=214
pool-1-thread-10, num=215
pool-1-thread-10, num=216
pool-1-thread-2, num=212
pool-1-thread-10, num=217
pool-1-thread-2, num=218
pool-1-thread-2, num=220
pool-1-thread-10, num=219
pool-1-thread-2, num=221
pool-1-thread-10, num=222
pool-1-thread-10, num=224
pool-1-thread-2, num=223
pool-1-thread-2, num=226
pool-1-thread-10, num=225
pool-1-thread-2, num=227
pool-1-thread-2, num=228
pool-1-thread-11, num=229
pool-1-thread-11, num=230
pool-1-thread-11, num=231
pool-1-thread-11, num=232
pool-1-thread-11, num=233
pool-1-thread-11, num=234
pool-1-thread-11, num=235
pool-1-thread-1, num=236
pool-1-thread-11, num=237
pool-1-thread-1, num=238
pool-1-thread-11, num=239
pool-1-thread-1, num=240
pool-1-thread-1, num=241
pool-1-thread-1, num=242
pool-1-thread-1, num=243
pool-1-thread-1, num=244
pool-1-thread-1, num=245
pool-1-thread-1, num=246
pool-1-thread-1, num=247
pool-1-thread-11, num=248
pool-1-thread-7, num=249
pool-1-thread-7, num=250
pool-1-thread-7, num=251
pool-1-thread-12, num=252
pool-1-thread-7, num=253
pool-1-thread-12, num=254
pool-1-thread-12, num=257
pool-1-thread-12, num=258
pool-1-thread-12, num=259
pool-1-thread-12, num=260
pool-1-thread-5, num=256
pool-1-thread-7, num=255
pool-1-thread-5, num=262
pool-1-thread-5, num=264
pool-1-thread-5, num=265
pool-1-thread-12, num=261
pool-1-thread-5, num=266
pool-1-thread-5, num=268
pool-1-thread-5, num=269
pool-1-thread-5, num=270
pool-1-thread-5, num=271
pool-1-thread-5, num=272
pool-1-thread-7, num=263
pool-1-thread-7, num=273
pool-1-thread-12, num=267
pool-1-thread-12, num=275
pool-1-thread-12, num=276
pool-1-thread-7, num=274
pool-1-thread-7, num=277
pool-1-thread-7, num=278
pool-1-thread-3, num=279
pool-1-thread-3, num=280
pool-1-thread-3, num=281
pool-1-thread-3, num=282
pool-1-thread-3, num=283
pool-1-thread-3, num=284
pool-1-thread-3, num=285
pool-1-thread-3, num=286
pool-1-thread-3, num=287
pool-1-thread-3, num=288
pool-1-thread-8, num=289
pool-1-thread-8, num=290
pool-1-thread-8, num=291
pool-1-thread-8, num=292
pool-1-thread-8, num=293
pool-1-thread-8, num=294
pool-1-thread-8, num=295
pool-1-thread-8, num=296
pool-1-thread-8, num=297
pool-1-thread-8, num=298
pool-1-thread-6, num=299
pool-1-thread-6, num=300
pool-1-thread-6, num=301
pool-1-thread-6, num=302
pool-1-thread-6, num=303
pool-1-thread-6, num=304
pool-1-thread-6, num=305
pool-1-thread-6, num=306
pool-1-thread-6, num=307
pool-1-thread-6, num=308
pool-1-thread-13, num=309
pool-1-thread-13, num=310
pool-1-thread-13, num=311
pool-1-thread-13, num=312
pool-1-thread-13, num=313
pool-1-thread-13, num=314
pool-1-thread-13, num=315
pool-1-thread-13, num=316
pool-1-thread-13, num=317
pool-1-thread-13, num=318
pool-1-thread-9, num=319
pool-1-thread-9, num=320
pool-1-thread-9, num=321
pool-1-thread-9, num=322
pool-1-thread-9, num=323
pool-1-thread-9, num=324
pool-1-thread-9, num=325
pool-1-thread-9, num=326
pool-1-thread-9, num=327
pool-1-thread-9, num=328
pool-1-thread-14, num=329
pool-1-thread-14, num=330
pool-1-thread-14, num=331
pool-1-thread-14, num=332
pool-1-thread-14, num=333
pool-1-thread-14, num=334
pool-1-thread-14, num=335
pool-1-thread-14, num=336
pool-1-thread-14, num=337
pool-1-thread-14, num=338
pool-1-thread-4, num=339
pool-1-thread-4, num=340
pool-1-thread-4, num=341
pool-1-thread-4, num=342
pool-1-thread-4, num=343
pool-1-thread-4, num=344
pool-1-thread-4, num=345
pool-1-thread-4, num=346
pool-1-thread-4, num=347
pool-1-thread-4, num=348
pool-1-thread-10, num=349
pool-1-thread-10, num=350
pool-1-thread-10, num=351
pool-1-thread-10, num=352
pool-1-thread-10, num=353
pool-1-thread-10, num=354
pool-1-thread-10, num=355
pool-1-thread-10, num=356
pool-1-thread-10, num=357
pool-1-thread-10, num=358
pool-1-thread-2, num=359
pool-1-thread-2, num=360
pool-1-thread-2, num=361
pool-1-thread-2, num=362
pool-1-thread-2, num=363
pool-1-thread-2, num=364
pool-1-thread-2, num=365
pool-1-thread-2, num=366
pool-1-thread-2, num=367
pool-1-thread-2, num=368
pool-1-thread-1, num=369
pool-1-thread-1, num=370
pool-1-thread-1, num=371
pool-1-thread-1, num=372
pool-1-thread-1, num=373
pool-1-thread-1, num=374
pool-1-thread-1, num=375
pool-1-thread-1, num=376
pool-1-thread-1, num=377
pool-1-thread-1, num=378
pool-1-thread-15, num=379
pool-1-thread-15, num=380
pool-1-thread-15, num=381
pool-1-thread-15, num=382
pool-1-thread-15, num=383
pool-1-thread-15, num=384
pool-1-thread-15, num=385
pool-1-thread-15, num=386
pool-1-thread-15, num=387
pool-1-thread-15, num=388
pool-1-thread-11, num=389
pool-1-thread-11, num=390
pool-1-thread-11, num=391
pool-1-thread-11, num=392
pool-1-thread-11, num=393
pool-1-thread-11, num=394
pool-1-thread-11, num=395
pool-1-thread-11, num=396
pool-1-thread-11, num=397
pool-1-thread-11, num=398
pool-1-thread-5, num=399
pool-1-thread-5, num=400
pool-1-thread-5, num=401
pool-1-thread-5, num=402
pool-1-thread-5, num=403
pool-1-thread-5, num=404
pool-1-thread-5, num=405
pool-1-thread-5, num=406
pool-1-thread-5, num=407
pool-1-thread-5, num=408
pool-1-thread-12, num=409
pool-1-thread-12, num=410
pool-1-thread-12, num=411
pool-1-thread-12, num=412
pool-1-thread-12, num=413
pool-1-thread-12, num=414
pool-1-thread-12, num=415
pool-1-thread-12, num=416
pool-1-thread-12, num=417
pool-1-thread-12, num=418
pool-1-thread-7, num=419
pool-1-thread-7, num=420
pool-1-thread-7, num=421
pool-1-thread-7, num=422
pool-1-thread-7, num=423
pool-1-thread-7, num=424
pool-1-thread-7, num=425
pool-1-thread-7, num=426
pool-1-thread-7, num=427
pool-1-thread-7, num=428
pool-1-thread-16, num=429
pool-1-thread-16, num=430
pool-1-thread-16, num=431
pool-1-thread-16, num=432
pool-1-thread-16, num=433
pool-1-thread-16, num=434
pool-1-thread-16, num=435
pool-1-thread-16, num=436
pool-1-thread-16, num=437
pool-1-thread-16, num=438
pool-1-thread-3, num=439
pool-1-thread-3, num=440
pool-1-thread-3, num=441
pool-1-thread-3, num=442
pool-1-thread-3, num=443
pool-1-thread-3, num=444
pool-1-thread-3, num=445
pool-1-thread-3, num=446
pool-1-thread-3, num=447
pool-1-thread-3, num=448
pool-1-thread-8, num=449
pool-1-thread-8, num=450
pool-1-thread-8, num=451
pool-1-thread-8, num=452
pool-1-thread-8, num=453
pool-1-thread-8, num=454
pool-1-thread-8, num=455
pool-1-thread-8, num=456
pool-1-thread-8, num=457
pool-1-thread-8, num=458
pool-1-thread-6, num=459
pool-1-thread-6, num=460
pool-1-thread-6, num=461
pool-1-thread-6, num=462
pool-1-thread-6, num=463
pool-1-thread-6, num=464
pool-1-thread-6, num=465
pool-1-thread-6, num=466
pool-1-thread-6, num=467
pool-1-thread-6, num=468
pool-1-thread-6, num=469
pool-1-thread-6, num=470
pool-1-thread-6, num=471
pool-1-thread-6, num=472
pool-1-thread-6, num=473
pool-1-thread-6, num=474
pool-1-thread-6, num=475
pool-1-thread-6, num=476
pool-1-thread-6, num=477
pool-1-thread-6, num=478
pool-1-thread-9, num=479
pool-1-thread-9, num=480
pool-1-thread-9, num=481
pool-1-thread-9, num=482
pool-1-thread-9, num=483
pool-1-thread-9, num=484
pool-1-thread-9, num=485
pool-1-thread-9, num=486
pool-1-thread-9, num=487
pool-1-thread-9, num=488
pool-1-thread-17, num=489
pool-1-thread-17, num=490
pool-1-thread-17, num=491
pool-1-thread-17, num=492
pool-1-thread-17, num=493
pool-1-thread-17, num=494
pool-1-thread-17, num=495
pool-1-thread-17, num=496
pool-1-thread-17, num=497
pool-1-thread-17, num=498
pool-1-thread-14, num=499
pool-1-thread-14, num=500
pool-1-thread-14, num=501
pool-1-thread-14, num=502
pool-1-thread-14, num=503
pool-1-thread-14, num=504
pool-1-thread-14, num=505
pool-1-thread-14, num=506
pool-1-thread-14, num=507
pool-1-thread-14, num=508
pool-1-thread-4, num=509
pool-1-thread-4, num=510
pool-1-thread-4, num=511
pool-1-thread-4, num=512
pool-1-thread-4, num=513
pool-1-thread-4, num=514
pool-1-thread-4, num=515
pool-1-thread-4, num=516
pool-1-thread-4, num=517
pool-1-thread-4, num=518
pool-1-thread-10, num=519
pool-1-thread-10, num=520
pool-1-thread-10, num=521
pool-1-thread-10, num=522
pool-1-thread-10, num=523
pool-1-thread-10, num=524
pool-1-thread-10, num=525
pool-1-thread-10, num=526
pool-1-thread-10, num=527
pool-1-thread-10, num=528
pool-1-thread-2, num=529
pool-1-thread-2, num=530
pool-1-thread-2, num=531
pool-1-thread-2, num=532
pool-1-thread-2, num=533
pool-1-thread-2, num=534
pool-1-thread-2, num=535
pool-1-thread-2, num=536
pool-1-thread-2, num=537
pool-1-thread-2, num=538
pool-1-thread-1, num=539
pool-1-thread-1, num=540
pool-1-thread-1, num=541
pool-1-thread-1, num=542
pool-1-thread-1, num=543
pool-1-thread-1, num=544
pool-1-thread-1, num=545
pool-1-thread-1, num=546
pool-1-thread-1, num=547
pool-1-thread-1, num=548
pool-1-thread-15, num=549
pool-1-thread-15, num=550
pool-1-thread-15, num=551
pool-1-thread-15, num=552
pool-1-thread-15, num=553
pool-1-thread-15, num=554
pool-1-thread-15, num=555
pool-1-thread-15, num=556
pool-1-thread-15, num=557
pool-1-thread-15, num=558
pool-1-thread-18, num=559
pool-1-thread-18, num=560
pool-1-thread-18, num=561
pool-1-thread-18, num=562
pool-1-thread-18, num=563
pool-1-thread-18, num=564
pool-1-thread-18, num=565
pool-1-thread-18, num=566
pool-1-thread-18, num=567
pool-1-thread-18, num=568
pool-1-thread-11, num=569
pool-1-thread-11, num=570
pool-1-thread-11, num=571
pool-1-thread-11, num=572
pool-1-thread-11, num=573
pool-1-thread-11, num=574
pool-1-thread-11, num=575
pool-1-thread-11, num=576
pool-1-thread-11, num=577
pool-1-thread-11, num=578
pool-1-thread-5, num=579
pool-1-thread-5, num=580
pool-1-thread-5, num=581
pool-1-thread-5, num=582
pool-1-thread-5, num=583
pool-1-thread-5, num=584
pool-1-thread-5, num=585
pool-1-thread-5, num=586
pool-1-thread-5, num=587
pool-1-thread-5, num=588
pool-1-thread-12, num=589
pool-1-thread-12, num=590
pool-1-thread-12, num=591
pool-1-thread-12, num=592
pool-1-thread-12, num=593
pool-1-thread-12, num=594
pool-1-thread-12, num=595
pool-1-thread-12, num=596
pool-1-thread-12, num=597
pool-1-thread-12, num=598
pool-1-thread-7, num=599
pool-1-thread-7, num=600
pool-1-thread-7, num=601
pool-1-thread-7, num=602
pool-1-thread-7, num=603
pool-1-thread-7, num=604
pool-1-thread-7, num=605
pool-1-thread-7, num=606
pool-1-thread-7, num=607
pool-1-thread-7, num=608
pool-1-thread-19, num=609
pool-1-thread-19, num=610
pool-1-thread-19, num=611
pool-1-thread-19, num=612
pool-1-thread-19, num=613
pool-1-thread-19, num=614
pool-1-thread-19, num=615
pool-1-thread-19, num=616
pool-1-thread-19, num=617
pool-1-thread-19, num=618
pool-1-thread-16, num=619
pool-1-thread-16, num=620
pool-1-thread-16, num=621
pool-1-thread-16, num=622
pool-1-thread-16, num=623
pool-1-thread-16, num=624
pool-1-thread-16, num=625
pool-1-thread-16, num=626
pool-1-thread-16, num=627
pool-1-thread-16, num=628
pool-1-thread-3, num=629
pool-1-thread-3, num=630
pool-1-thread-3, num=631
pool-1-thread-3, num=632
pool-1-thread-3, num=633
pool-1-thread-3, num=634
pool-1-thread-3, num=635
pool-1-thread-3, num=636
pool-1-thread-3, num=637
pool-1-thread-3, num=638
pool-1-thread-8, num=639
pool-1-thread-8, num=640
pool-1-thread-8, num=641
pool-1-thread-8, num=642
pool-1-thread-8, num=643
pool-1-thread-8, num=644
pool-1-thread-8, num=645
pool-1-thread-8, num=646
pool-1-thread-8, num=647
pool-1-thread-8, num=648
pool-1-thread-6, num=649
pool-1-thread-6, num=650
pool-1-thread-6, num=651
pool-1-thread-6, num=652
pool-1-thread-6, num=653
pool-1-thread-6, num=654
pool-1-thread-6, num=655
pool-1-thread-6, num=656
pool-1-thread-6, num=657
pool-1-thread-6, num=658
pool-1-thread-13, num=659
pool-1-thread-13, num=660
pool-1-thread-13, num=661
pool-1-thread-13, num=662
pool-1-thread-13, num=663
pool-1-thread-13, num=664
pool-1-thread-13, num=665
pool-1-thread-13, num=666
pool-1-thread-13, num=667
pool-1-thread-13, num=668
pool-1-thread-9, num=669
pool-1-thread-9, num=670
pool-1-thread-9, num=671
pool-1-thread-9, num=672
pool-1-thread-9, num=673
pool-1-thread-9, num=674
pool-1-thread-9, num=675
pool-1-thread-9, num=676
pool-1-thread-9, num=677
pool-1-thread-9, num=678
pool-1-thread-20, num=679
pool-1-thread-20, num=680
pool-1-thread-20, num=681
pool-1-thread-20, num=682
pool-1-thread-20, num=683
pool-1-thread-20, num=684
pool-1-thread-20, num=685
pool-1-thread-20, num=686
pool-1-thread-20, num=687
pool-1-thread-20, num=688
pool-1-thread-17, num=689
pool-1-thread-17, num=690
pool-1-thread-17, num=691
pool-1-thread-17, num=692
pool-1-thread-17, num=693
pool-1-thread-17, num=694
pool-1-thread-17, num=695
pool-1-thread-17, num=696
pool-1-thread-17, num=697
pool-1-thread-17, num=698
pool-1-thread-14, num=699
pool-1-thread-14, num=700
pool-1-thread-14, num=701
pool-1-thread-14, num=702
pool-1-thread-14, num=703
pool-1-thread-14, num=704
pool-1-thread-14, num=705
pool-1-thread-14, num=706
pool-1-thread-14, num=707
pool-1-thread-14, num=708
pool-1-thread-4, num=709
pool-1-thread-4, num=710
pool-1-thread-4, num=711
pool-1-thread-4, num=712
pool-1-thread-4, num=713
pool-1-thread-4, num=714
pool-1-thread-4, num=715
pool-1-thread-4, num=716
pool-1-thread-4, num=717
pool-1-thread-4, num=718
pool-1-thread-10, num=719
pool-1-thread-10, num=720
pool-1-thread-10, num=721
pool-1-thread-10, num=722
pool-1-thread-10, num=723
pool-1-thread-10, num=724
pool-1-thread-10, num=725
pool-1-thread-10, num=726
pool-1-thread-10, num=727
pool-1-thread-10, num=728
pool-1-thread-2, num=729
pool-1-thread-2, num=730
pool-1-thread-2, num=731
pool-1-thread-2, num=732
pool-1-thread-2, num=733
pool-1-thread-2, num=734
pool-1-thread-2, num=735
pool-1-thread-2, num=736
pool-1-thread-2, num=737
pool-1-thread-2, num=738
pool-1-thread-1, num=739
pool-1-thread-1, num=740
pool-1-thread-1, num=741
pool-1-thread-1, num=742
pool-1-thread-1, num=743
pool-1-thread-1, num=744
pool-1-thread-1, num=745
pool-1-thread-1, num=746
pool-1-thread-1, num=747
pool-1-thread-1, num=748
pool-1-thread-21, num=749
pool-1-thread-21, num=750
pool-1-thread-21, num=751
pool-1-thread-21, num=752
pool-1-thread-21, num=753
pool-1-thread-21, num=754
pool-1-thread-21, num=755
pool-1-thread-21, num=756
pool-1-thread-21, num=757
pool-1-thread-21, num=758
pool-1-thread-15, num=759
pool-1-thread-15, num=760
pool-1-thread-15, num=761
pool-1-thread-15, num=762
pool-1-thread-15, num=763
pool-1-thread-15, num=764
pool-1-thread-15, num=765
pool-1-thread-15, num=766
pool-1-thread-15, num=767
pool-1-thread-15, num=768
pool-1-thread-18, num=769
pool-1-thread-18, num=770
pool-1-thread-18, num=771
pool-1-thread-18, num=772
pool-1-thread-18, num=773
pool-1-thread-18, num=774
pool-1-thread-18, num=775
pool-1-thread-18, num=776
pool-1-thread-18, num=777
pool-1-thread-18, num=778
pool-1-thread-11, num=779
pool-1-thread-11, num=780
pool-1-thread-11, num=781
pool-1-thread-11, num=782
pool-1-thread-11, num=783
pool-1-thread-11, num=784
pool-1-thread-11, num=785
pool-1-thread-11, num=786
pool-1-thread-11, num=787
pool-1-thread-11, num=788
pool-1-thread-22, num=789
pool-1-thread-22, num=790
pool-1-thread-22, num=791
pool-1-thread-22, num=792
pool-1-thread-22, num=793
pool-1-thread-22, num=794
pool-1-thread-22, num=795
pool-1-thread-22, num=796
pool-1-thread-22, num=797
pool-1-thread-22, num=798
pool-1-thread-5, num=799
pool-1-thread-5, num=800
pool-1-thread-5, num=801
pool-1-thread-5, num=802
pool-1-thread-5, num=803
pool-1-thread-5, num=804
pool-1-thread-5, num=805
pool-1-thread-5, num=806
pool-1-thread-5, num=807
pool-1-thread-5, num=808
pool-1-thread-23, num=809
pool-1-thread-23, num=810
pool-1-thread-23, num=811
pool-1-thread-23, num=812
pool-1-thread-23, num=813
pool-1-thread-23, num=814
pool-1-thread-23, num=815
pool-1-thread-23, num=816
pool-1-thread-23, num=817
pool-1-thread-23, num=818
pool-1-thread-12, num=819
pool-1-thread-12, num=820
pool-1-thread-12, num=821
pool-1-thread-12, num=822
pool-1-thread-12, num=823
pool-1-thread-12, num=824
pool-1-thread-12, num=825
pool-1-thread-12, num=826
pool-1-thread-12, num=827
pool-1-thread-12, num=828
pool-1-thread-7, num=829
pool-1-thread-7, num=830
pool-1-thread-7, num=831
pool-1-thread-7, num=832
pool-1-thread-7, num=833
pool-1-thread-7, num=834
pool-1-thread-7, num=835
pool-1-thread-7, num=836
pool-1-thread-7, num=837
pool-1-thread-7, num=838
pool-1-thread-24, num=839
pool-1-thread-24, num=840
pool-1-thread-24, num=841
pool-1-thread-24, num=842
pool-1-thread-24, num=843
pool-1-thread-24, num=844
pool-1-thread-24, num=845
pool-1-thread-24, num=846
pool-1-thread-24, num=847
pool-1-thread-24, num=848
pool-1-thread-24, num=849
pool-1-thread-24, num=850
pool-1-thread-24, num=851
pool-1-thread-24, num=852
pool-1-thread-24, num=853
pool-1-thread-24, num=854
pool-1-thread-24, num=855
pool-1-thread-24, num=856
pool-1-thread-24, num=857
pool-1-thread-24, num=858
pool-1-thread-19, num=859
pool-1-thread-19, num=860
pool-1-thread-19, num=861
pool-1-thread-19, num=862
pool-1-thread-19, num=863
pool-1-thread-19, num=864
pool-1-thread-19, num=865
pool-1-thread-19, num=866
pool-1-thread-19, num=867
pool-1-thread-19, num=868
pool-1-thread-3, num=869
pool-1-thread-3, num=870
pool-1-thread-3, num=871
pool-1-thread-3, num=872
pool-1-thread-3, num=873
pool-1-thread-3, num=874
pool-1-thread-3, num=875
pool-1-thread-3, num=876
pool-1-thread-3, num=877
pool-1-thread-3, num=878
pool-1-thread-25, num=879
pool-1-thread-25, num=880
pool-1-thread-25, num=881
pool-1-thread-25, num=882
pool-1-thread-25, num=883
pool-1-thread-25, num=884
pool-1-thread-25, num=885
pool-1-thread-25, num=886
pool-1-thread-25, num=887
pool-1-thread-25, num=888
pool-1-thread-8, num=889
pool-1-thread-8, num=890
pool-1-thread-8, num=891
pool-1-thread-8, num=892
pool-1-thread-8, num=893
pool-1-thread-8, num=894
pool-1-thread-8, num=895
pool-1-thread-8, num=896
pool-1-thread-8, num=897
pool-1-thread-8, num=898
pool-1-thread-6, num=899
pool-1-thread-6, num=900
pool-1-thread-6, num=901
pool-1-thread-6, num=902
pool-1-thread-6, num=903
pool-1-thread-6, num=904
pool-1-thread-6, num=905
pool-1-thread-6, num=906
pool-1-thread-6, num=907
pool-1-thread-6, num=908
pool-1-thread-13, num=909
pool-1-thread-13, num=910
pool-1-thread-13, num=911
pool-1-thread-26, num=912
pool-1-thread-26, num=914
pool-1-thread-26, num=915
pool-1-thread-26, num=916
pool-1-thread-26, num=917
pool-1-thread-26, num=918
pool-1-thread-26, num=919
pool-1-thread-26, num=920
pool-1-thread-26, num=921
pool-1-thread-26, num=922
pool-1-thread-13, num=913
pool-1-thread-13, num=923
pool-1-thread-13, num=924
pool-1-thread-13, num=925
pool-1-thread-13, num=926
pool-1-thread-13, num=927
pool-1-thread-13, num=928
pool-1-thread-9, num=929
pool-1-thread-9, num=930
pool-1-thread-9, num=931
pool-1-thread-9, num=932
pool-1-thread-9, num=933
pool-1-thread-9, num=934
pool-1-thread-9, num=935
pool-1-thread-9, num=936
pool-1-thread-9, num=937
pool-1-thread-9, num=938
pool-1-thread-20, num=939
pool-1-thread-20, num=940
pool-1-thread-20, num=941
pool-1-thread-20, num=942
pool-1-thread-20, num=943
pool-1-thread-20, num=944
pool-1-thread-20, num=945
pool-1-thread-20, num=946
pool-1-thread-20, num=947
pool-1-thread-20, num=948
pool-1-thread-17, num=949
pool-1-thread-17, num=950
pool-1-thread-17, num=951
pool-1-thread-17, num=952
pool-1-thread-17, num=953
pool-1-thread-17, num=954
pool-1-thread-17, num=955
pool-1-thread-17, num=956
pool-1-thread-17, num=957
pool-1-thread-17, num=958
pool-1-thread-27, num=959
pool-1-thread-27, num=960
pool-1-thread-27, num=961
pool-1-thread-27, num=962
pool-1-thread-27, num=963
pool-1-thread-27, num=964
pool-1-thread-27, num=965
pool-1-thread-27, num=966
pool-1-thread-27, num=967
pool-1-thread-27, num=968
pool-1-thread-14, num=969
pool-1-thread-14, num=970
pool-1-thread-14, num=971
pool-1-thread-14, num=972
pool-1-thread-14, num=973
pool-1-thread-14, num=974
pool-1-thread-14, num=975
pool-1-thread-14, num=976
pool-1-thread-14, num=977
pool-1-thread-14, num=978
pool-1-thread-4, num=979
pool-1-thread-4, num=980
pool-1-thread-4, num=981
pool-1-thread-4, num=982
pool-1-thread-4, num=983
pool-1-thread-4, num=984
pool-1-thread-4, num=985
pool-1-thread-4, num=986
pool-1-thread-4, num=987
pool-1-thread-4, num=988
pool-1-thread-10, num=989
pool-1-thread-10, num=990
pool-1-thread-10, num=991
pool-1-thread-10, num=992
pool-1-thread-10, num=993
pool-1-thread-10, num=994
pool-1-thread-10, num=995
pool-1-thread-10, num=996
pool-1-thread-10, num=997
pool-1-thread-10, num=998
쓰레드 콜 종료
executor has been terminated

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

 

thread가 1~27 까지 27개의 쓰레드만 실행이 되었습니다. 왜 이런 걸까요?

corePoolSize를 1로 주었기 때문에 쓰레드풀은 1개의 쓰레드로 시작을 하게되고 해당 쓰레드가 끝나기 전에 새로운 task의 실행 요청이 들어오면 새로운 쓰레드를 생성해서 처리하게 됩니다. 하지만 새로운 task의 실행 요청이 이전 task가 종료된 이후에 들어오게되면 새로운 쓰레드를 생성하지 않고 기존의 쓰레드를 재사용하게 됩니다. 이것이 바로 쓰레드 풀을 사용하는 장점이죠. 쓰레드 풀에 필요한 개수만큼의 쓰레드만 생성하고 재사용함으로써 불필요한 쓰레드를 만듬으로써 소모되는 메모리를 줄일 수 있습니다.

 

여기까지 ThreadPoolExecutor를 활용한 멀티쓰레드 구현에 대해 간략한 포스팅을 마칩니다.

 

Reference: 오라클 Java 8 공식 문서

 

추가로 현재 Thread 클래스를 상속한 Task에서 num 변수를 1씩 증가해서 출력하도록 하였습니다만, 실제로 마지막 숫자를 보면 999가 아닌 998이 출력되었습니다. 여러 번 실행해보면 가장 큰 숫자가 99X~999가 랜덤하게 출력되는 것을 확인 할 수 있습니다. 이것은 num이라는 변수를 모든 쓰레드가 공유하는데 값을 1씩 증가시키는 오퍼레이션이 thread safe하게 동작하고 있지 않음을 보여주고 있는 것입니다. 이것을 해결하려면 volatile키워드를 사용하면 됩니다.

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를 설정 및 사용하는 방법