這兩天看阿里的JAVA開發手冊,到多線程的時候說永遠不要用 new Thread()這種方式來使用多線程。確實是這樣的,我一直在用線程池,到了springboot才發現他已經給咱們提供了很方便的線程池機制。
本博客代碼託管在github上 https://github.com/gxz0422042...
Spring
是經過任務執行器(TaskExecutor
)來實現多線程和併發編程,使用ThreadPoolTaskExecutor
來建立一個基於線城池的TaskExecutor
。在使用線程池的大多數狀況下都是異步非阻塞的。咱們配置註解@EnableAsync
能夠開啓異步任務。而後在實際執行的方法上配置註解@Async
上聲明是異步任務。java
配置類代碼以下:git
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 @EnableAsync public 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; } }
解讀:github
EnableAsync
來開啓Springboot
對於異步任務的支持AsyncConfigurator
,返回一個ThreadPoolTaskExecutor
線程池對象。任務執行代碼:spring
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 */ @Service public 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) @SpringBootTest public class SpringbootLearnApplicationTests { @Autowired private AsyncTaskService asyncTaskService; @Test public void contextLoads() { } @Test public void threadTest() { for (int i = 0; i < 20; i++) { asyncTaskService.executeAsyncTask(i); } } }
測試結果:springboot
線程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