TIL

Thread Pool

서버 성능 기본 지표 2개: 응답 시간, 처리량

TPS를 높이려면

서버, 스레드 풀 늘리기 한계

톰캣 스레드 풀 튜닝

acceptCount

maxConnections

maxThread

maxThreads vs. maxConnections

Thread Pool

자바에서 스레드는 운영체제의 리소스인 system-level 스레드에 매핑된다. 만약 스레드를 무한으로 생성한다면 리소스는 빠르게 소진될 것이다.

스레드 풀은 멀티 스레드 환경에서 리소스를 적약하고 미리 정의된 제한에서 병렬 처리를 하도록 도와준다. img.png

자바에서의 스레드 풀

ThreadPoolExecutor executor = 
  (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
executor.submit(() -> {
    Thread.sleep(1000);
    return null;
});
executor.submit(() -> {
    Thread.sleep(1000);
    return null;
});
executor.submit(() -> {
    Thread.sleep(1000);
    return null;
});

assertEquals(2, executor.getPoolSize());
assertEquals(1, executor.getQueue().size());
ThreadPoolExecutor executor = 
  (ThreadPoolExecutor) Executors.newCachedThreadPool();
executor.submit(() -> {
    Thread.sleep(1000);
    return null;
});
executor.submit(() -> {
    Thread.sleep(1000);
    return null;
});
executor.submit(() -> {
    Thread.sleep(1000);
    return null;
});

assertEquals(3, executor.getPoolSize());
assertEquals(0, executor.getQueue().size());

https://ehdvudee.tistory.com/30

https://www.baeldung.com/thread-pool-java-and-guava

https://parkadd.tistory.com/114

https://sas-study.tistory.com/231