spring线程池的使用示例,测试拒绝任务和任务装饰器

本文通过一个Java程序展示了如何配置和使用Spring的ThreadPoolTaskExecutor,包括核心线程数、最大线程数、阻塞队列类型、线程工厂和拒绝策略。在测试中,当任务超过线程池容量时,观察到执行了自定义的拒绝策略并抛出了异常。
摘要由CSDN通过智能技术生成

corePoolSize:核心线程数。 maximumPoolSize:最大线程数。表明线程中最多能够创建的线程数量。
keepAliveTime:空闲的线程保留的时间。 unit:空闲线程的保留时间单位。 BlockingQueue
workQueue:用于保存等待执行的任务的阻塞队列。可以选择以下几个阻塞队列。
1、ArrayBlockingQueue:是一个基于数组结构的有界阻塞队列,此队列按 FIFO(先进先出)原则对元素进行排序。
2、LinkedBlockingQueue:一个基于链表结构的阻塞队列,此队列按FIFO (先进先出)
排序元素,吞吐量通常要高于ArrayBlockingQueue。静态工厂方法Executors.newFixedThreadPool()使用了这个队列
3、SynchronousQueue:一个不存储元素的阻塞队列。每个插入操作必须等到另一个线程调用移除操作,否则插入操作一直处于阻塞状态,吞吐量通常要高于LinkedBlockingQueue,静态工厂方法Executors.newCachedThreadPool使用了这个队列。
4、PriorityBlockingQueue:一个具有优先级的无限阻塞队列。
threadFactory:用于设置创建线程的工厂,可以通过线程工厂给每个创建出来的线程做些更有意义的事情,比如设置daemon和优先级等等
handler:饱和策略处理器。默认提供的4中策略上面已经有解释了

通过测试方法,来测试线程池的拒绝任务和装饰器任务的使用

package com.myzy.auth.reetrantlock;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 测试spring线程池,包装了JDK的ThreadPoolExecutor 提供了默认的最大线程池
 */
public class TestThreadPoolTaskExecutor {
    public static void main(String[] args) {
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        threadPoolExecutor.setAllowCoreThreadTimeOut(true);
        threadPoolExecutor.setCorePoolSize(2);
        threadPoolExecutor.setMaxPoolSize(4);
        threadPoolExecutor.setQueueCapacity(10);
        threadPoolExecutor.setTaskDecorator(runnable -> {//线程任务的装饰器
            System.out.println("执行任务前执行,装饰线程任务");
            return () -> {
                runnable.run();
                System.out.println("执行线程后执行");
            };
        });
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//抛出异常来拒绝
        threadPoolExecutor.initialize();

        for (int i = 0; i < 10000; i++) {
            threadPoolExecutor.execute(() -> {
                try {
                    Thread.sleep(1000L);
                    System.out.println("任务执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}

执行结果

"C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.3\lib\idea_rt.jar=55291:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_231\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\rt.jar;D:\work\code\auth\target\classes;D:\work\repository\org\springframework\cloud\spring-cloud-openfeign-core\2.2.5.RELEASE\spring-cloud-openfeign-core-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.3.RELEASE\spring-boot-autoconfigure-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot\2.2.3.RELEASE\spring-boot-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\cloud\spring-cloud-netflix-ribbon\2.2.5.RELEASE\spring-cloud-netflix-ribbon-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\cloud\spring-cloud-netflix-archaius\2.2.5.RELEASE\spring-cloud-netflix-archaius-2.2.5.RELEASE.jar;D:\work\repository\io\github\openfeign\form\feign-form-spring\3.8.0\feign-form-spring-3.8.0.jar;D:\work\repository\io\github\openfeign\form\feign-form\3.8.0\feign-form-3.8.0.jar;D:\work\repository\commons-fileupload\commons-fileupload\1.4\commons-fileupload-1.4.jar;D:\work\repository\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\work\repository\org\springframework\cloud\spring-cloud-starter-openfeign\2.2.5.RELEASE\spring-cloud-starter-openfeign-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\cloud\spring-cloud-starter\2.2.5.RELEASE\spring-cloud-starter-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;D:\work\repository\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;D:\work\repository\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;D:\work\repository\org\springframework\spring-web\5.2.3.RELEASE\spring-web-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\cloud\spring-cloud-commons\2.2.5.RELEASE\spring-cloud-commons-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\security\spring-security-crypto\5.2.1.RELEASE\spring-security-crypto-5.2.1.RELEASE.jar;D:\work\repository\io\github\openfeign\feign-core\10.10.1\feign-core-10.10.1.jar;D:\work\repository\io\github\openfeign\feign-slf4j\10.10.1\feign-slf4j-10.10.1.jar;D:\work\repository\io\github\openfeign\feign-hystrix\10.10.1\feign-hystrix-10.10.1.jar;D:\work\repository\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\work\repository\com\google\code\findbugs\jsr305\3.0.1\jsr305-3.0.1.jar;D:\work\repository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\work\repository\com\netflix\hystrix\hystrix-core\1.5.18\hystrix-core-1.5.18.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-web\2.2.3.RELEASE\spring-boot-starter-web-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot-starter\2.2.3.RELEASE\spring-boot-starter-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-logging\2.2.3.RELEASE\spring-boot-starter-logging-2.2.3.RELEASE.jar;D:\work\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\work\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\work\repository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;D:\work\repository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;D:\work\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\work\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\work\repository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-json\2.2.3.RELEASE\spring-boot-starter-json-2.2.3.RELEASE.jar;D:\work\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.2\jackson-datatype-jdk8-2.10.2.jar;D:\work\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.2\jackson-datatype-jsr310-2.10.2.jar;D:\work\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.2\jackson-module-parameter-names-2.10.2.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-tomcat\2.2.3.RELEASE\spring-boot-starter-tomcat-2.2.3.RELEASE.jar;D:\work\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.30\tomcat-embed-core-9.0.30.jar;D:\work\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.30\tomcat-embed-el-9.0.30.jar;D:\work\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.30\tomcat-embed-websocket-9.0.30.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-validation\2.2.3.RELEASE\spring-boot-starter-validation-2.2.3.RELEASE.jar;D:\work\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;D:\work\repository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;D:\work\repository\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;D:\work\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;D:\work\repository\org\springframework\spring-webmvc\5.2.3.RELEASE\spring-webmvc-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-context\5.2.3.RELEASE\spring-context-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-expression\5.2.3.RELEASE\spring-expression-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-actuator\2.2.3.RELEASE\spring-boot-starter-actuator-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot-actuator-autoconfigure\2.2.3.RELEASE\spring-boot-actuator-autoconfigure-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\boot\spring-boot-actuator\2.2.3.RELEASE\spring-boot-actuator-2.2.3.RELEASE.jar;D:\work\repository\io\micrometer\micrometer-core\1.3.2\micrometer-core-1.3.2.jar;D:\work\repository\org\hdrhistogram\HdrHistogram\2.1.11\HdrHistogram-2.1.11.jar;D:\work\repository\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\work\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-discovery\2.2.3.RELEASE\spring-cloud-starter-alibaba-nacos-discovery-2.2.3.RELEASE.jar;D:\work\repository\com\alibaba\nacos\nacos-client\1.3.3\nacos-client-1.3.3.jar;D:\work\repository\com\alibaba\nacos\nacos-common\1.3.3\nacos-common-1.3.3.jar;D:\work\repository\org\apache\httpcomponents\httpasyncclient\4.1.4\httpasyncclient-4.1.4.jar;D:\work\repository\org\apache\httpcomponents\httpcore\4.4.13\httpcore-4.4.13.jar;D:\work\repository\org\apache\httpcomponents\httpcore-nio\4.4.13\httpcore-nio-4.4.13.jar;D:\work\repository\com\alibaba\nacos\nacos-api\1.3.3\nacos-api-1.3.3.jar;D:\work\repository\org\apache\commons\commons-lang3\3.9\commons-lang3-3.9.jar;D:\work\repository\com\google\guava\guava\29.0-jre\guava-29.0-jre.jar;D:\work\repository\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;D:\work\repository\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;D:\work\repository\org\checkerframework\checker-qual\2.11.1\checker-qual-2.11.1.jar;D:\work\repository\com\google\errorprone\error_prone_annotations\2.3.4\error_prone_annotations-2.3.4.jar;D:\work\repository\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;D:\work\repository\commons-codec\commons-codec\1.13\commons-codec-1.13.jar;D:\work\repository\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\work\repository\com\alibaba\spring\spring-context-support\1.0.10\spring-context-support-1.0.10.jar;D:\work\repository\org\springframework\cloud\spring-cloud-context\2.2.5.RELEASE\spring-cloud-context-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.2.5.RELEASE\spring-cloud-starter-netflix-ribbon-2.2.5.RELEASE.jar;D:\work\repository\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.2.5.RELEASE\spring-cloud-starter-netflix-archaius-2.2.5.RELEASE.jar;D:\work\repository\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;D:\work\repository\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;D:\work\repository\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;D:\work\repository\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;D:\work\repository\javax\inject\javax.inject\1\javax.inject-1.jar;D:\work\repository\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;D:\work\repository\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;D:\work\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\work\repository\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;D:\work\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\work\repository\org\apache\httpcomponents\httpclient\4.5.10\httpclient-4.5.10.jar;D:\work\repository\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\work\repository\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\work\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\work\repository\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\work\repository\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\work\repository\com\netflix\netflix-commons\netflix-commons-util\0.3.0\netflix-commons-util-0.3.0.jar;D:\work\repository\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;D:\work\repository\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;D:\work\repository\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;D:\work\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.2.3.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.2.3.RELEASE.jar;D:\work\repository\org\projectlombok\lombok\1.18.10\lombok-1.18.10.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-jdbc\2.2.3.RELEASE\spring-boot-starter-jdbc-2.2.3.RELEASE.jar;D:\work\repository\com\zaxxer\HikariCP\3.4.2\HikariCP-3.4.2.jar;D:\work\repository\org\springframework\spring-jdbc\5.2.3.RELEASE\spring-jdbc-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-tx\5.2.3.RELEASE\spring-tx-5.2.3.RELEASE.jar;D:\work\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;D:\work\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;D:\work\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\work\repository\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;D:\work\repository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-data-redis\2.2.3.RELEASE\spring-boot-starter-data-redis-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\data\spring-data-redis\2.2.4.RELEASE\spring-data-redis-2.2.4.RELEASE.jar;D:\work\repository\org\springframework\data\spring-data-keyvalue\2.2.4.RELEASE\spring-data-keyvalue-2.2.4.RELEASE.jar;D:\work\repository\org\springframework\spring-oxm\5.2.3.RELEASE\spring-oxm-5.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-context-support\5.2.3.RELEASE\spring-context-support-5.2.3.RELEASE.jar;D:\work\repository\io\lettuce\lettuce-core\5.2.1.RELEASE\lettuce-core-5.2.1.RELEASE.jar;D:\work\repository\io\netty\netty-common\4.1.45.Final\netty-common-4.1.45.Final.jar;D:\work\repository\io\netty\netty-handler\4.1.45.Final\netty-handler-4.1.45.Final.jar;D:\work\repository\io\netty\netty-buffer\4.1.45.Final\netty-buffer-4.1.45.Final.jar;D:\work\repository\io\netty\netty-codec\4.1.45.Final\netty-codec-4.1.45.Final.jar;D:\work\repository\io\netty\netty-transport\4.1.45.Final\netty-transport-4.1.45.Final.jar;D:\work\repository\io\netty\netty-resolver\4.1.45.Final\netty-resolver-4.1.45.Final.jar;D:\work\repository\io\projectreactor\reactor-core\3.3.2.RELEASE\reactor-core-3.3.2.RELEASE.jar;D:\work\repository\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-data-mongodb\2.2.3.RELEASE\spring-boot-starter-data-mongodb-2.2.3.RELEASE.jar;D:\work\repository\org\mongodb\mongodb-driver\3.11.2\mongodb-driver-3.11.2.jar;D:\work\repository\org\mongodb\bson\3.11.2\bson-3.11.2.jar;D:\work\repository\org\mongodb\mongodb-driver-core\3.11.2\mongodb-driver-core-3.11.2.jar;D:\work\repository\org\springframework\data\spring-data-mongodb\2.2.4.RELEASE\spring-data-mongodb-2.2.4.RELEASE.jar;D:\work\repository\org\springframework\data\spring-data-commons\2.2.4.RELEASE\spring-data-commons-2.2.4.RELEASE.jar;D:\work\repository\org\apache\shiro\shiro-spring\1.3.2\shiro-spring-1.3.2.jar;D:\work\repository\org\apache\shiro\shiro-core\1.3.2\shiro-core-1.3.2.jar;D:\work\repository\commons-beanutils\commons-beanutils\1.8.3\commons-beanutils-1.8.3.jar;D:\work\repository\org\apache\shiro\shiro-web\1.3.2\shiro-web-1.3.2.jar;D:\work\repository\com\xuxueli\xxl-job-core\2.3.0\xxl-job-core-2.3.0.jar;D:\work\repository\io\netty\netty-all\4.1.45.Final\netty-all-4.1.45.Final.jar;D:\work\repository\com\google\code\gson\gson\2.8.6\gson-2.8.6.jar;D:\work\repository\org\codehaus\groovy\groovy\2.5.9\groovy-2.5.9.jar;D:\work\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\work\repository\org\springframework\boot\spring-boot-starter-aop\2.2.3.RELEASE\spring-boot-starter-aop-2.2.3.RELEASE.jar;D:\work\repository\org\springframework\spring-aop\5.2.3.RELEASE\spring-aop-5.2.3.RELEASE.jar;D:\work\repository\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;D:\work\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-sentinel\2.1.1.RELEASE\spring-cloud-starter-alibaba-sentinel-2.1.1.RELEASE.jar;D:\work\repository\com\alibaba\cloud\spring-cloud-alibaba-sentinel\2.1.1.RELEASE\spring-cloud-alibaba-sentinel-2.1.1.RELEASE.jar;D:\work\repository\com\alibaba\csp\sentinel-transport-simple-http\1.8.0\sentinel-transport-simple-http-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-transport-common\1.8.0\sentinel-transport-common-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-datasource-extension\1.8.0\sentinel-datasource-extension-1.8.0.jar;D:\work\repository\com\alibaba\fastjson\1.2.71\fastjson-1.2.71.jar;D:\work\repository\com\alibaba\csp\sentinel-annotation-aspectj\1.8.0\sentinel-annotation-aspectj-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-core\1.8.0\sentinel-core-1.8.0.jar;D:\work\repository\org\aspectj\aspectjrt\1.9.5\aspectjrt-1.9.5.jar;D:\work\repository\com\alibaba\csp\sentinel-web-servlet\1.8.0\sentinel-web-servlet-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-spring-webflux-adapter\1.8.0\sentinel-spring-webflux-adapter-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-reactor-adapter\1.8.0\sentinel-reactor-adapter-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-parameter-flow-control\1.8.0\sentinel-parameter-flow-control-1.8.0.jar;D:\work\repository\com\googlecode\concurrentlinkedhashmap\concurrentlinkedhashmap-lru\1.4.2\concurrentlinkedhashmap-lru-1.4.2.jar;D:\work\repository\com\alibaba\csp\sentinel-cluster-server-default\1.8.0\sentinel-cluster-server-default-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-cluster-common-default\1.8.0\sentinel-cluster-common-default-1.8.0.jar;D:\work\repository\com\alibaba\csp\sentinel-cluster-client-default\1.8.0\sentinel-cluster-client-default-1.8.0.jar;D:\work\repository\com\alibaba\cloud\spring-cloud-alibaba-sentinel-datasource\2.2.3.RELEASE\spring-cloud-alibaba-sentinel-datasource-2.2.3.RELEASE.jar;D:\work\repository\com\fasterxml\jackson\core\jackson-core\2.10.0\jackson-core-2.10.0.jar;D:\work\repository\com\fasterxml\jackson\core\jackson-databind\2.10.0\jackson-databind-2.10.0.jar;D:\work\repository\com\fasterxml\jackson\core\jackson-annotations\2.10.2\jackson-annotations-2.10.2.jar" com.myzy.auth.reetrantlock.TestThreadPoolTaskExecutor
22:02:23.623 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
执行任务前执行,装饰线程任务
Exception in thread "main" org.springframework.core.task.TaskRejectedException: Executor [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor$1@42d3bd8b[Running, pool size = 4, active threads = 4, queued tasks = 10, completed tasks = 0]] did not accept task: com.myzy.auth.reetrantlock.TestThreadPoolTaskExecutor$$Lambda$2/762218386@26ba2a48
	at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:317)
	at com.myzy.auth.reetrantlock.TestThreadPoolTaskExecutor.main(TestThreadPoolTaskExecutor.java:28)
Caused by: java.util.concurrent.RejectedExecutionException: Task com.myzy.auth.reetrantlock.TestThreadPoolTaskExecutor$$Lambda$3/672320506@dfd3711 rejected from org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor$1@42d3bd8b[Running, pool size = 4, active threads = 4, queued tasks = 10, completed tasks = 0]
	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
	at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
	at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
	at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor$1.execute(ThreadPoolTaskExecutor.java:238)
	at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:314)
	... 1 more
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行
任务执行
执行线程后执行

Process finished with exit code 130

通过测试,以及执行结果可以看到,我们设置的拒绝策略和装饰器任务都执行了,希望在生产业务中有所使用和帮助!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值