ThreadPoolExecutor 소개

5184 단어 JavaExecutor
1.소개 Executor 프레임 워 크 는 스 레 드 탱크 의 관 리 를 포함 하고 스 레 드 공장,대기 열 과 거부 전략 등 을 제공 하 며 Executor 프레임 워 크 는 병행 프로 그래 밍 을 더욱 간단하게 한다.스 레 드 풀 구현 클래스 Thread PoolExecutor 는 Executor 프레임 워 크 의 가장 핵심 적 인 클래스 입 니 다.2.간단 한 실례
(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

좋은 웹페이지 즐겨찾기