欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring之线程池集成

程序员文章站 2022-05-04 17:16:13
...

spring之线程池集成

        spring的线程池技术使用的是ThreadPoolTaskExecutor,当我们点进ThreadPoolTaskExecutor这个类的时候,可以看见里面是对于JDK中的ThreadPoolExecutor的封装:

spring之线程池集成

可见,ThreadPoolExecutor是ThreadPoolTaskExecutor类的一个属性.而其他几个属性正好也是ThreadPoolTaskExecutor的几个重要参数:

        CorePoolSize:                        线程池维护线程的最小数量,默认值为1.

        MaxPoolSize:                         线程池维护线程的最大数量,默认值为2的32次方-1,即2147483647.

        KeepAliveSeconds:                空闲线程的存活时间,默认值是60s.

        QueueCapacity:                     队列的最大长度,默认值为2的32次方-1,即2147483647.

        AllowCoreThreadTimeOut:    是否允许核心线程空闲退出,默认是false.

        RejectedExecutionHandler:      拒绝策略.这是ExecutorConfigurationSupport类中的方法,他是ThreadPoolExecutor的父类.

拒绝策略有以下四种:

        ThreadPoolExecutor.AbortPolicy:                丢弃任务并抛出RejectedExecutionException异常.(默认使用)

        ThreadPoolExecutor.DiscardPolicy:             丢弃任务,但是不不抛出异常.

        ThreadPoolExecutor.DiscardOldestPolicy:   丢弃队列最前面的任务,然后重新尝试执行.

        ThreadPoolExecutor.CallerRunsPolicy:        有调用线程处理该任务.

        下面直接上代码:

                首先是异步任务类:

package com.yczuoxin.demo.config;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class AsyncTask {
    /** 表示异步执行 */
    @Async
    public void executeAsyncTask(Integer i){
        System.out.println("执行异步任务: " + i);
    }

    /** 表示异步执行 */
    @Async
    public void specialExecuteAsyncTask(Integer i){
        System.out.println("执行特殊异步任务: " + i);
    }
}

                下面是配置类:

package com.yczuoxin.demo.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
/** 开启异步支持 */
@EnableAsync
@ComponentScan("com.yczuoxin.demo.config")
public class TaskExecutorConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}

                最后是测试类:

package com.yczuoxin.demo.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TaskTest {
    public static void main(String[] args) {

        int count = 10;

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
        AsyncTask task = context.getBean(AsyncTask.class);
        for (int i = 0; i < count; i++) {
            task.executeAsyncTask(i);
            task.specialExecuteAsyncTask(i);
        }

        context.close();
    }
}

                测试结果如下图:

spring之线程池集成

        首先可以看见执行的结果并不是有序的.在执行一遍结果又不相同.异步线程池创建成功.

相关标签: 线程池