怎么在春季启动中对多线程进行配置

  介绍

这篇文章给大家介绍怎么在春季启动中对多线程进行配置,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1,配置线程配置类

package 测试;      import  java.util.concurrent.Executor;      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;      @ configuration   @ComponentScan (“test")   @EnableAsync//,线程配置类   public  class  AsyncTaskConfig  implements  AsyncConfigurer  {//才能,ThredPoolTaskExcutor的处理流程//才能,当池子大小小于corePoolSize,就新建线程,并处理请求//才能,当池子大小等于corePoolSize,把请求放入工作队列中,池子里的空闲线程就去工作队列中取任务并处理//才能,当工作队列放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理//才能,当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁      @Override才能   public 才能;Executor  getAsyncExecutor (), {   ,,,ThreadPoolTaskExecutor  taskExecutor =, new  ThreadPoolTaskExecutor ();   ,,,taskExecutor.setCorePoolSize(5);//,最小线程数   ,,,taskExecutor.setMaxPoolSize(10);//,最大线程数   ,,,taskExecutor.setQueueCapacity(25);//,等待队列      ,,,taskExecutor.initialize ();      ,,,return  taskExecutor;   ,,}      @Override才能   public 才能;AsyncUncaughtExceptionHandler  getAsyncUncaughtExceptionHandler (), {   ,,,return 零;   ,,}   }

2,定义线程执行任务类

package 测试;      import  java.util.Random;   import  java.util.concurrent.Future;      import  org.springframework.scheduling.annotation.Async;   import  org.springframework.scheduling.annotation.AsyncResult;   import  org.springframework.stereotype.Service;      @ service//,线程执行任务类   public  class  AsyncTaskService  {      Random 才能;Random =, new 随机();//,默认构造方法      @Async才能//才能,表明是异步方法//才能,无返回值   public 才能;void  executeAsyncTask (Integer 我),{   ,,,System.out.println(“执行异步任务:“,+,i);   ,,}      ,/* *   ,,*,异常调用返回的未来   ,,*,   ,,* @param 我   ,,* @return   ,,*,@throws  InterruptedException   ,,*/@Async才能   public 才能;Future< String>, asyncInvokeReturnFuture (int 我),throws  InterruptedException  {   ,,,System.out.println (“input  is “, +, i);   ,,,thread . sleep (1000, *, random.nextInt(我));      ,,,Future, future =, new  AsyncResult(“成功:“,+,i);//,未来接收返回值,这里是字符串类型,可以指明其他类型      ,,,return 未来;   ,,}   }

3,调用

package 测试;      import  java.util.ArrayList;   import 并不知道;   import  java.util.concurrent.ExecutionException;   import  java.util.concurrent.Future;      import  org.springframework.context.annotation.AnnotationConfigApplicationContext;   import  org.springframework.core.task.TaskRejectedException;      public  class  Application  {      public 才能;static  void  main (String [], args), throws  InterruptedException, ExecutionException  {   ,,,//,testVoid ();      ,,,testReturn ();   ,,}//,才能测试无返回结果   private 才能static  void  testVoid (), {   ,,,AnnotationConfigApplicationContext  context =, new 所(AsyncTaskConfig.class);   ,,,AsyncTaskService  AsyncTaskService =, context.getBean (AsyncTaskService.class);      ,,,//,创建了20个线程   ,,,for  (int 小姐:=,1;,小姐:& lt;=, 20;,我+ +),{   ,,,,,asyncTaskService.executeAsyncTask(我);   ,,,}      ,,,context.close ();   ,,}//,才能测试有返回结果   private 才能static  void  testReturn (), throws  InterruptedException, ExecutionException  {   ,,,AnnotationConfigApplicationContext  context =, new 所(AsyncTaskConfig.class);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在春季启动中对多线程进行配置