前言
springt通過任務(wù)執(zhí)行器(taskexecutor)來實(shí)現(xiàn)多線程和并發(fā)編程。使用threadpooltaskexecutor可實(shí)現(xiàn)一個基于線程池的taskexecutor。而實(shí)際開發(fā)中任務(wù)一般是非阻礙的,即異步的,所以我們要在配置類中通過@enableasync 開啟對異步任務(wù)的支持,并通過實(shí)際執(zhí)行bean的方法中使用@async注解來聲明其是一個異步任務(wù)。
基于springboot的多線程程序開發(fā)過程中,由于本身也需要注入spring容器進(jìn)行管理,才能發(fā)揮springboot的優(yōu)勢。所以這篇文字主要用來記錄開發(fā)中兩者結(jié)合時(shí)需要注意的一些事項(xiàng)。
注意事項(xiàng)
第一步我們把線程類的實(shí)例注入sping容器進(jìn)行管理
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
|
@configuration @springbootapplication @import ({threadconfig. class }) public class threadapp implements commandlinerunner { public static void main(string[] args) throws exception { applicationcontext app = springapplication.run(threadapp . class , args); //這里主要保存上下文對象實(shí)例,需要加上。springbootutils類網(wǎng)上很多,可以自己搜下 springbootutils.setapplicationcontext(app); } //access command line arguments @override public void run(string... args) throws exception { //do something } } //componentscan注解會掃描com.demo.thead下,也就是多線程類所在的包下的文件 @configuration @componentscan (basepackages = { "com.demo.thread" }) public class threadconfig{ } |
這里使用springboot @import 注解,把threadconfig里掃描到的包中帶注解的示例,如@component等注入到spring容器當(dāng)中.
然后是線程的啟動,這里在我的業(yè)務(wù)場景中有兩種情況:
1、程序運(yùn)行時(shí),自動啟動;
這在一般的可執(zhí)行程序里面,當(dāng)然可以直接在main函數(shù)里執(zhí)行通過代碼啟動線程。但在springboot中,我們可以使用@postconstruct注解的方式,讓已經(jīng)注入bean容器的線程對象自啟動
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@component public class demothread extends thread { //注意這里,如果你沒有實(shí)現(xiàn)把多線程類的實(shí)例注入到spring容器中,這里你是無法拿到其他自動裝配的對象實(shí)例的的,這也是我們第一步的意義所在。 @autowired private xxxservice xxxservice; @postconstruct public void start() { super .start(); } public void run() { // ok,在這里你就可以實(shí)現(xiàn)線程要實(shí)現(xiàn)的功能邏輯了,自然也可以直接使用裝配好的sevice對象實(shí)例。 } } |
2、在程序中,需要開啟線程時(shí)啟動,比如在從kafka接收數(shù)據(jù),開啟線程處理,當(dāng)然這種情況下也需要通過第一步,把線程類實(shí)例注入到sping容器中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private taskthread thread; private executorservice taskpool= new threadpoolexecutor( 5 , 10 , 1000 , timeunit.milliseconds, new arrayblockingqueue<>( 10 ), new threadpoolexecutor.callerrunspolicy()); @kafkalistener (topics = "xxtopic" ) public void receive(consumerrecord<object, object> consumerrecord) { jsonobject json = json.parseobject(consumerrecord.value().tostring()); //通過springbootutils獲取線程類的實(shí)例 thread = springbootutils.getbean(taskthread. class ); //啟動線程 //new thread(thread).start() ; //向線程對象里傳值 thread.init(i); //放入線程池執(zhí)行 taskpool.execute(thread); } |
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
|
//注意這里是否添加@scope("prototype")注解 @component @scope ( "prototype" ) public class taskthread implements runnable{ protected int value= 0 ; @autowired private xxxservice xxxservice; //threadlocal 對象,單例模式下可以保證成員變量的線程安全和獨(dú)立性。 public threadlocal<integer> valuelocal = new threadlocal < integer > () { @override protected integer initialvalue() { return 0 ; } }; protected static final logger log = loggerfactory.getlogger(gpstaskthread. class ); @override public final void run() { try { log.info(value+ "" ); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } public void init( int value) { this .value=value; } } |
在這里我們需要注意,taskthread這個線程類在spirngboot中是否要添加@scope("prototype")
注解設(shè)置為多例模式還是默認(rèn)單例模式。
在單例模式下springbootutils.getbean(taskthread.class)
每次返回的都是同一個對象,雖然不需要每次都創(chuàng)建新的對象,但無法保證成員變量的線程安全,也就是說在線程池中的執(zhí)行的線程,它們的value值是共享的。而多例模式下,由于每次創(chuàng)建的都是一個新的線程對象,則不存在上述問題。
所以在這里請大家注意無論是我上面的示例代碼還是平常的web開發(fā)中,spirngboot默認(rèn)為單例模式,自定義的成員變量是線程不安全的,需要通過threadlocal 或這其他方法做同步處理。
回到我們當(dāng)前的業(yè)務(wù)場景,在這里我們需要每個線程處理的value值不同,互不影響,那么通過@scope("prototype")
注解把taskthread設(shè)置為多例模式。
總結(jié)
通過上面的示例,我們可以看到springboot與多線程的結(jié)合還是比較簡單,通過配置,我們既可以在spring容器中管理線程類,也可以在線程中使用sping容器中的對象實(shí)例。同時(shí)我們在使用的過程當(dāng)中要有意識的去注意線程安全方面的問題和內(nèi)部運(yùn)行機(jī)制的問題。當(dāng)然這里理解的還是比較淺顯,如果有不正確的地方還請大家指出與海涵。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/dafanjoy/p/9692841.html