這兩天看阿里的JAVA開發手冊,到多線程的時候說永遠不要用 new Thread()這種方式來使用多線程。確實是這樣的,我一直在用線程池,到了springboot才發現他已經給我們提供了很方便的線程池機制。
本博客代碼托管在github上https://github.com/gxz0422042...
一、介紹
Spring是通過任務執行器(TaskExecutor)來實現多線程和并發編程,使用ThreadPoolTaskExecutor來創建一個基于線城池的TaskExecutor。在使用線程池的大多數情況下都是異步非阻塞的。我們配置注解@EnableAsync可以開啟異步任務。然后在實際執行的方法上配置注解@Async上聲明是異步任務。
二、配置類
配置類代碼如下:
package com.spartajet.springbootlearn.thread;import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;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;/** * @description * @create 2017-02-22 下午11:53 * @email gxz04220427@163.com */@Configuration@EnableAsyncpublic class ThreadConfig implements AsyncConfigurer { /** * The {@link Executor} instance to be used when processing async * method invocations. */ @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(15); executor.setQueueCapacity(25); executor.initialize(); return executor; } /** * The {@link AsyncUncaughtExceptionHandler} instance to be used * when an exception is thrown during an asynchronous method execution * with {@code void} return type. */ @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; }}
解讀:
利用EnableAsync來開啟Springboot對于異步任務的支持
配置類實現接口AsyncConfigurator,返回一個ThreadPoolTaskExecutor線程池對象。
三、任務執行
任務執行代碼:
package com.spartajet.springbootlearn.thread;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;/** * @description * @create 2017-02-23 上午12:00 * @email gxz04220427@163.com */@Servicepublic class AsyncTaskService { @Async public void executeAsyncTask(int i) { System.out.println("線程" + Thread.currentThread().getName() + " 執行異步任務:" + i); }}
代碼解讀:
通過@Async注解表明該方法是異步方法,如果注解在類上,那表明這個類里面的所有方法都是異步的。
四、測試代碼
package com.spartajet.springbootlearn;import com.spartajet.springbootlearn.thread.AsyncTaskService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith (SpringRunner.class)@SpringBootTestpublic class SpringbootLearnApplicationTests { @Autowired private AsyncTaskService asyncTaskService; @Test public void contextLoads() { } @Test public void threadTest() { for (int i = 0; i < 20; i++) { asyncTaskService.executeAsyncTask(i); } }}
測試結果:
線程ThreadPoolTaskExecutor-4 執行異步任務:3
線程ThreadPoolTaskExecutor-2 執行異步任務:1
線程ThreadPoolTaskExecutor-1 執行異步任務:0
線程ThreadPoolTaskExecutor-1 執行異步任務:7
線程ThreadPoolTaskExecutor-1 執行異步任務:8
線程ThreadPoolTaskExecutor-1 執行異步任務:9
線程ThreadPoolTaskExecutor-1 執行異步任務:10
線程ThreadPoolTaskExecutor-5 執行異步任務:4
線程ThreadPoolTaskExecutor-3 執行異步任務:2
線程ThreadPoolTaskExecutor-5 執行異步任務:12
線程ThreadPoolTaskExecutor-1 執行異步任務:11
線程ThreadPoolTaskExecutor-2 執行異步任務:6
線程ThreadPoolTaskExecutor-4 執行異步任務:5
線程ThreadPoolTaskExecutor-2 執行異步任務:16
線程ThreadPoolTaskExecutor-1 執行異步任務:15
線程ThreadPoolTaskExecutor-5 執行異步任務:14
線程ThreadPoolTaskExecutor-3 執行異步任務:13
線程ThreadPoolTaskExecutor-1 執行異步任務:19
線程ThreadPoolTaskExecutor-2 執行異步任務:18
線程ThreadPoolTaskExecutor-4 執行異步任務:17
總結
以上所述是小編給大家介紹的Springboot對多線程的支持,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選