這兩天看阿里的JAVA開發(fā)手冊,到多線程的時候說永遠(yuǎn)不要用 new Thread()這種方式來使用多線程。確實(shí)是這樣的,我一直在用線程池,到了springboot才發(fā)現(xiàn)他已經(jīng)給我們提供了很方便的線程池機(jī)制。
本博客代碼托管在github上https://github.com/gxz0422042...
一、介紹
Spring是通過任務(wù)執(zhí)行器(TaskExecutor)來實(shí)現(xiàn)多線程和并發(fā)編程,使用ThreadPoolTaskExecutor來創(chuàng)建一個基于線城池的TaskExecutor。在使用線程池的大多數(shù)情況下都是異步非阻塞的。我們配置注解@EnableAsync可以開啟異步任務(wù)。然后在實(shí)際執(zhí)行的方法上配置注解@Async上聲明是異步任務(wù)。
二、配置類
配置類代碼如下:
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對于異步任務(wù)的支持
配置類實(shí)現(xiàn)接口AsyncConfigurator,返回一個ThreadPoolTaskExecutor線程池對象。
三、任務(wù)執(zhí)行
任務(wù)執(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 */@Servicepublic class AsyncTaskService { @Async public void executeAsyncTask(int i) { System.out.println("線程" + Thread.currentThread().getName() + " 執(zhí)行異步任務(wù):" + 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); } }}
測試結(jié)果:
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務(wù):3
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務(wù):1
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):0
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):7
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):8
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):9
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):10
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務(wù):4
線程ThreadPoolTaskExecutor-3 執(zhí)行異步任務(wù):2
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務(wù):12
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):11
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務(wù):6
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務(wù):5
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務(wù):16
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):15
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務(wù):14
線程ThreadPoolTaskExecutor-3 執(zhí)行異步任務(wù):13
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務(wù):19
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務(wù):18
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務(wù):17
總結(jié)
以上所述是小編給大家介紹的Springboot對多線程的支持,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!
|
新聞熱點(diǎn)
疑難解答
圖片精選