詳解Springboot對多線程的支持
這兩天看阿里的JAVA開發(fā)手冊,到多線程的時候說永遠不要用 new Thread()這種方式來使用多線程。確實是這樣的,我一直在用線程池,到了springboot才發(fā)現(xiàn)他已經(jīng)給我們提供了很方便的線程池機制。
本博客代碼托管在github上https://github.com/gxz0422042...
一、介紹
Spring是通過任務執(zhí)行器(TaskExecutor)來實現(xiàn)多線程和并發(fā)編程,使用ThreadPoolTaskExecutor來創(chuàng)建一個基于線城池的TaskExecutor。在使用線程池的大多數(shù)情況下都是異步非阻塞的。我們配置注解@EnableAsync可以開啟異步任務。然后在實際執(zhí)行的方法上配置注解@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 @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; } }
解讀:
利用EnableAsync來開啟Springboot對于異步任務的支持
配置類實現(xiàn)接口AsyncConfigurator,返回一個ThreadPoolTaskExecutor線程池對象。
三、任務執(zhí)行
任務執(zhí)行代碼:
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() + " 執(zhí)行異步任務:" + 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); } } }
測試結(jié)果:
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務:3
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:1
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:0
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:7
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:8
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:9
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:10
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務:4
線程ThreadPoolTaskExecutor-3 執(zhí)行異步任務:2
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務:12
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:11
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:6
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務:5
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:16
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:15
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務:14
線程ThreadPoolTaskExecutor-3 執(zhí)行異步任務:13
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:19
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:18
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務:17
總結(jié)
以上所述是小編給大家介紹的Springboot對多線程的支持,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
spring-cloud入門之eureka-client(服務注冊)
本篇文章主要介紹了spring-cloud入門之eureka-client(服務注冊),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01SpringBoot中@EnableAutoConfiguration注解的實現(xiàn)
Spring Boot@EnableAutoConfiguration是一個強大的工具,可以簡化配置過程,從而實現(xiàn)快速開發(fā),本文主要介紹了SpringBoot中@EnableAutoConfiguration注解的實現(xiàn),感興趣的可以了解一下2024-01-01