ThreadPoolExecutor简介

1、简介
Executor框架包括了线程池的管理,提供了线程工厂、队列以及拒绝策略等,Executor框架让并发编程变得更加简单。线程池实现类ThreadPoolExecutor是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

 

你可能感兴趣的:(Java,Java,Executor)