ThreadPoolExecutor 소개
(1)MyRunnable.java
package cn.hwd.tpe.service;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyRunnable implements Runnable {
	
	@Override
	public void run() {
		log.info(Thread.currentThread().getName());
        try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			log.error(e.getMessage());
		}
	}
	
}
(2)Application.java
package cn.hwd.tpe;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import cn.hwd.tpe.service.MyRunnable;
@SpringBootApplication
public class Application implements CommandLineRunner {
	private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final Long KEEP_ALIVE_TIME = 1L;
    
	public static void main(String[] args) throws Exception {
		SpringApplication.run(Application.class, args);
    }
	@Override
	public void run(String... args) throws Exception {
		//  ThreadPoolExecutor                  
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
        		CORE_POOL_SIZE, //          
                MAX_POOL_SIZE, //          
                KEEP_ALIVE_TIME, //            ,              
                TimeUnit.SECONDS, //    
                new ArrayBlockingQueue<>(QUEUE_CAPACITY), //    ,             
                new ThreadPoolExecutor.CallerRunsPolicy());//    ,                ,             
        for (int i = 0; i < 10; i++) {
            //  WorkerThread  (WorkerThread    Runnable   )
            Runnable worker = new MyRunnable();
            //  Runnable
            executor.execute(worker);
        }
        //     
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
	}
	
}
3.실행 결과
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.20.RELEASE)
2019-12-17 11:14:10.092  INFO 9172 --- [           main] cn.hwd.tpe.Application                   : Starting Application on LAPTOP-N1UHE4RC with PID 9172 (D:\workspace\work\test\thread-pool-executor\bin started by vineg in D:\workspace\work\test\thread-pool-executor)
2019-12-17 11:14:10.096  INFO 9172 --- [           main] cn.hwd.tpe.Application                   : No active profile set, falling back to default profiles: default
2019-12-17 11:14:10.146  INFO 9172 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@757acd7b: startup date [Tue Dec 17 11:14:10 CST 2019]; root of context hierarchy
2019-12-17 11:14:10.726  INFO 9172 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-12-17 11:14:10.741  INFO 9172 --- [pool-1-thread-1] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-1
2019-12-17 11:14:10.742  INFO 9172 --- [pool-1-thread-2] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-2
2019-12-17 11:14:10.744  INFO 9172 --- [pool-1-thread-3] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-3
2019-12-17 11:14:10.749  INFO 9172 --- [pool-1-thread-4] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-4
2019-12-17 11:14:10.751  INFO 9172 --- [pool-1-thread-5] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-5
2019-12-17 11:14:15.742  INFO 9172 --- [pool-1-thread-1] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-1
2019-12-17 11:14:15.743  INFO 9172 --- [pool-1-thread-2] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-2
2019-12-17 11:14:15.746  INFO 9172 --- [pool-1-thread-3] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-3
2019-12-17 11:14:15.751  INFO 9172 --- [pool-1-thread-4] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-4
2019-12-17 11:14:15.752  INFO 9172 --- [pool-1-thread-5] cn.hwd.tpe.service.MyRunnable            : pool-1-thread-5
2019-12-17 11:14:20.755  INFO 9172 --- [           main] cn.hwd.tpe.Application                   : Started Application in 10.966 seconds (JVM running for 11.431)
2019-12-17 11:14:20.756  INFO 9172 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@757acd7b: startup date [Tue Dec 17 11:14:10 CST 2019]; root of context hierarchy
2019-12-17 11:14:20.758  INFO 9172 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.