Springboot 多線程


  • Spring 通過任務執行器(TaskExecutor)來實現多線程和並發編程,使用 ThreadPoolTaskExecutor 可實現一個基於線程池的 TaskExecutor。 而實際開發中任務一般是非阻礙的,即異步的,所以我們要在配置類中通過 @EnbaleAsync 開啟對異步任務的支持,並通過在實際執行的 Bean 的方法中使用 @Async 注解來聲明其是一個異步任務
  • 示例:
    • 配置類

      @Configuration
      @EnableAsync
      public class TaskExecutorConfig {
      
          @Bean("taskExexutor")
          public Executor getAsyncExecutors(){
              ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
              executor.setCorePoolSize(5);
              executor.setQueueCapacity(25);
              executor.setMaxPoolSize(10);
              executor.initialize();
              return executor;
          }
      }
      
    • 任務執行類

      @Component
      public class TaskService {
          Logger logger = LoggerFactory.getLogger(TaskService.class);
      
          @Async("taskExexutor")
          public void doTask(int i) {
              String name = Thread.currentThread().getName();
              String content = String.format("發送短信方法---- %s", name);
              System.out.println("執行的順序:" + i + " " + content + "   執行開始");
              for (int j = 0; j < 100000; j++) {
                  doSomething(j);
              }
              System.out.println(content + "   執行結束");
          }
      
          private void doSomething(int i) {
      //        try {
      ////            Thread.sleep(10);
      //        } catch (InterruptedException e) {
      //            e.printStackTrace();
      //        }
          }
      
          @Async("taskExexutor")
          public void doTask2() {
              String name = Thread.currentThread().getName();
              String content = String.format("發送短信方法---- %s", name);
              System.out.println(content + "   執行開始");
              for (int i = 0; i < 100000; i++) {
                  doSomething(i);
              }
              System.out.println(content + "   執行結束");
          }
      }
      
    • 運行

      @Component
      public class OrderTaskService {
      
          @Autowired
          private TaskService taskService;
      
          public void orderTask(int i)  {
              taskService.doTask(i);
      //        taskService.doTask2();
          }
      }
      
    • 測試類

      @RunWith(SpringRunner.class)
      @SpringBootTest(classes = Ch522Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
      public class TaskTest {
      
          @Autowired
          private OrderTaskService orderTaskService;
      
      
          @Test
          public void executorTest() {
              for (int i = 0; i < 15; i++) {
                  orderTaskService.orderTask(i);
              }
          }
      }
      
    • 測試結果

      執行的順序:4 發送短信方法---- taskExexutor-5   執行開始
      執行的順序:0 發送短信方法---- taskExexutor-1   執行開始
      執行的順序:3 發送短信方法---- taskExexutor-4   執行開始
      執行的順序:2 發送短信方法---- taskExexutor-3   執行開始
      執行的順序:1 發送短信方法---- taskExexutor-2   執行開始
      發送短信方法---- taskExexutor-5   執行結束
      發送短信方法---- taskExexutor-1   執行結束
      執行的順序:5 發送短信方法---- taskExexutor-5   執行開始
      發送短信方法---- taskExexutor-4   執行結束
      執行的順序:7 發送短信方法---- taskExexutor-4   執行開始
      2019-09-04 22:19:49.710  INFO 13988 --- [       Thread-3] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
      發送短信方法---- taskExexutor-3   執行結束
      發送短信方法---- taskExexutor-2   執行結束
      執行的順序:9 發送短信方法---- taskExexutor-2   執行開始
      執行的順序:6 發送短信方法---- taskExexutor-1   執行開始
      2019-09-04 22:19:49.718  INFO 13988 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'taskExexutor'
      發送短信方法---- taskExexutor-4   執行結束
      發送短信方法---- taskExexutor-5   執行結束
      執行的順序:8 發送短信方法---- taskExexutor-3   執行開始
      發送短信方法---- taskExexutor-2   執行結束
      發送短信方法---- taskExexutor-3   執行結束
      發送短信方法---- taskExexutor-1   執行結束
      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM