@Configuration
@EnableAsync
public class ThreadPoolConfig {@Bean(name = "zpPool")public ThreadPoolExecutor zpPool() {return new ThreadPoolExecutor(//核心线程数5,//最大线程数5,60,TimeUnit.SECONDS,//队列大小new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE),//定义线程名称new ThreadFactory() {private final AtomicInteger mThreadNum = new AtomicInteger(1);@Overridepublic Thread newThread(Runnable r) {return new Thread(r, "zpPool-" + mThreadNum.getAndIncrement());}},//拒绝策略new ThreadPoolExecutor.AbortPolicy());}
}
@RestController 
@Api ( tags =  "线程池中多线程的测试" ) 
public  class  manyXcTest { @Resource testAsync testAscy; @PostMapping ( "/testAscy" ) @ApiOperation ( "自定义线程池的测试" ) @AuthPassport public  Result  testAsync ( )  { testAscy. testAscy ( ) ; return  new  Result ( ) ; } } 
@Service 
public  class  testAsync { @SneakyThrows @Async ( "zpPool" ) public  void  testAscy ( )  { int  i =  0 ; i++ ; int  j= 0 ; System . out. println ( "多线程线程池任务测试 "  +  i) ; for  (  j =  0 ;  j <  10 ;  j++ )  { System . out. println ( "多线程线程池任务测试 "  +  j+ "开始" ) ; } System . out. println ( "多线程线程池任务测试 "  +  j+ "结束" ) ; synchronized ( this ) { Thread . sleep ( 60000 ) ; System . out. println ( "多线程线程池任务测试 "  +  j+ "结束" ) ; } } } ## 在分布式部署的情况下还有一种方式能够进行保证线程使用的稳定性```java@XxlJob ( value =  "zpJob" ,  init =  "init" ,  destroy =  "destroy" ) public  void  zpJob1 ( )   { XxlJobHelper . log ( "zpJob开始执行" ) ; XxlJobHelper . log ( "zpJob结束执行" ) ; } }