前言
springt通過任務執行器(taskexecutor)來實現多線程和并發編程。使用threadpooltaskexecutor可實現一個基于線程池的taskexecutor。而實際開發中任務一般是非阻礙的,即異步的,所以我們要在配置類中通過@enableasync 開啟對異步任務的支持,并通過實際執行bean的方法中使用@async注解來聲明其是一個異步任務。
基于springboot的多線程程序開發過程中,由于本身也需要注入spring容器進行管理,才能發揮springboot的優勢。所以這篇文字主要用來記錄開發中兩者結合時需要注意的一些事項。
注意事項
第一步我們把線程類的實例注入sping容器進行管理
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); //這里主要保存上下文對象實例,需要加上。springbootutils類網上很多,可以自己搜下 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容器當中.
然后是線程的啟動,這里在我的業務場景中有兩種情況:
1、程序運行時,自動啟動;
這在一般的可執行程序里面,當然可以直接在main函數里執行通過代碼啟動線程。但在springboot中,我們可以使用@postconstruct注解的方式,讓已經注入bean容器的線程對象自啟動
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@component public class demothread extends thread { //注意這里,如果你沒有實現把多線程類的實例注入到spring容器中,這里你是無法拿到其他自動裝配的對象實例的的,這也是我們第一步的意義所在。 @autowired private xxxservice xxxservice; @postconstruct public void start() { super .start(); } public void run() { // ok,在這里你就可以實現線程要實現的功能邏輯了,自然也可以直接使用裝配好的sevice對象實例。 } } |
2、在程序中,需要開啟線程時啟動,比如在從kafka接收數據,開啟線程處理,當然這種情況下也需要通過第一步,把線程類實例注入到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獲取線程類的實例 thread = springbootutils.getbean(taskthread. class ); //啟動線程 //new thread(thread).start() ; //向線程對象里傳值 thread.init(i); //放入線程池執行 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 對象,單例模式下可以保證成員變量的線程安全和獨立性。 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")
注解設置為多例模式還是默認單例模式。
在單例模式下springbootutils.getbean(taskthread.class)
每次返回的都是同一個對象,雖然不需要每次都創建新的對象,但無法保證成員變量的線程安全,也就是說在線程池中的執行的線程,它們的value值是共享的。而多例模式下,由于每次創建的都是一個新的線程對象,則不存在上述問題。
所以在這里請大家注意無論是我上面的示例代碼還是平常的web開發中,spirngboot默認為單例模式,自定義的成員變量是線程不安全的,需要通過threadlocal 或這其他方法做同步處理。
回到我們當前的業務場景,在這里我們需要每個線程處理的value值不同,互不影響,那么通過@scope("prototype")
注解把taskthread設置為多例模式。
總結
通過上面的示例,我們可以看到springboot與多線程的結合還是比較簡單,通過配置,我們既可以在spring容器中管理線程類,也可以在線程中使用sping容器中的對象實例。同時我們在使用的過程當中要有意識的去注意線程安全方面的問題和內部運行機制的問題。當然這里理解的還是比較淺顯,如果有不正確的地方還請大家指出與海涵。
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/dafanjoy/p/9692841.html