這兩天看阿里的java開發手冊,到多線程的時候說永遠不要用 new thread()這種方式來使用多線程。確實是這樣的,我一直在用線程池,到了springboot才發現他已經給我們提供了很方便的線程池機制。
本博客代碼托管在github上https://github.com/gxz0422042...
一、介紹
spring是通過任務執行器(taskexecutor)來實現多線程和并發編程,使用threadpooltaskexecutor來創建一個基于線城池的taskexecutor。在使用線程池的大多數情況下都是異步非阻塞的。我們配置注解@enableasync可以開啟異步任務。然后在實際執行的方法上配置注解@async上聲明是異步任務。
二、配置類
配置類代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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對于異步任務的支持
配置類實現接口asyncconfigurator,返回一個threadpooltaskexecutor線程池對象。
三、任務執行
任務執行代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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注解表明該方法是異步方法,如果注解在類上,那表明這個類里面的所有方法都是異步的。
四、測試代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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); } } } |
測試結果:
線程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對多線程的支持,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://segmentfault.com/a/1190000015766938