thread-per-message模式(這項工作就交給你了)
當你很忙碌的時候,這個時候公司樓下有個快遞,于是你委托你的同事幫你拿一下你的快遞,這樣你就可以繼續(xù)做自己的工作了
在thread-per-message模式中,消息的委托端和執(zhí)行端是不同的線程,消息的委托端會告訴執(zhí)行端線程,這個工作就交給你了
host類:
針對請求創(chuàng)建線程的類,主要通過開啟新的線程,調(diào)用helper的handle,并將要打印的文字傳遞。
1
2
3
4
5
6
7
8
9
10
11
12
|
public class host { private final helper helper = new helper(); public void request( final int count, final char c){ system.out.println( "request開始" ); new thread(){ public void run(){ helper.handle(count, c); } }.start(); system.out.println( "request結(jié)束" ); } } |
helper類:
提供字符顯示的功能,slowly方法模擬打印耗時
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class helper { public void handle( int count , char c){ system.out.println( "handle方法開始" ); for ( int i= 0 ;i<count;i++){ slowly(); system.out.print(c); } system.out.println( "" ); system.out.println( "handle方法結(jié)束" ); } private void slowly(){ try { thread.sleep( 100 ); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } |
main類:
創(chuàng)建host的實例,并調(diào)用request的方法
1
2
3
4
5
6
7
8
|
public static void main(string[] args) { system.out.println( "main begin" ); host host = new host(); host.request( 10 , 'a' ); host.request( 20 , 'b' ); host.request( 30 , 'c' ); system.out.println( "main end" ); } |
測試結(jié)果:
main begin
request方法開始了
request方法結(jié)束
request方法開始了
request方法結(jié)束
request方法開始了
request方法結(jié)束
main end
handle方法開始
handle方法開始
handle方法開始
bacbacacbacbacbacbacbacbacba
handle方法結(jié)束
cbcbcbcbcbcbcbcbcbcbcb
handle方法結(jié)束
cccccccccc
handle方法結(jié)束
從運行的結(jié)果可以看出,request方法,并沒有等待handle方法執(zhí)行結(jié)束后再執(zhí)行,而是調(diào)用handle方法后就返回到request方法中,直到運行結(jié)束,所以相當于request方法將所要進行的打印一定數(shù)量字符的工作轉(zhuǎn)交給了handle方法,而request方法則可以再執(zhí)行笨方法中的其他的語句,不必等待handle方法完成。這也同時告訴我們,當某些工作比較耗時時,則可以通過這種模式啟動新的線程來執(zhí)行處理。可以將此模式應(yīng)用于服務(wù)器,這樣就可以減少服務(wù)器的響應(yīng)時間。
講解一下進程和線程:
線程和進程最大的區(qū)別就是內(nèi)存是否共存。
每個進程有自己的獨立的內(nèi)存空間,一個進程不可以擅自讀取和寫入其他的進程的內(nèi)存,由于進程的內(nèi)存空間是彼此獨立的,所以一個進程無需擔心被其他的進程所破壞。
線程之間是可以共存的,一個線程向?qū)嵗袑懭雰?nèi)容,其他線程就可以讀取該實例的內(nèi)容,由于多個線程可以訪問同一個實例,我們就需要保證其正確執(zhí)行互斥處理。
host設(shè)計優(yōu)化:
1.使用java.util.concurrent包下的threadfactory接口設(shè)計host類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class host { public void request( final int count, final char c){ system.out.println( "request方法開始了" ); threadfactory.newthread( new runnable() { @override public void run() { // todo auto-generated method stub helper.handle(count, c); } } ).start();; system.out.println( "request方法結(jié)束" ); } } |
對應(yīng)的host實例化對象:
host host = new host(executors.defaultthreadfactory());
這樣設(shè)計的優(yōu)勢在于,原來的使用new創(chuàng)建的實例代碼依賴于java.lang.thread類,無法控制創(chuàng)建線程的部分,可復(fù)用性較低,假如使用threadfactory來保存對應(yīng)類的對象,調(diào)用newthread方法創(chuàng)建新的線程,這樣便實現(xiàn)了線程的創(chuàng)建,這樣不再依賴于thread類,而是取決于構(gòu)造函數(shù)中傳入的threadfactory對象,實現(xiàn)了控制線程創(chuàng)建的細節(jié)。
使用java.util.concurrent.executor接口重新設(shè)計host類:
前面的threadfactory接口隱藏了線程創(chuàng)建的細節(jié),但是并未隱藏線程創(chuàng)建的操作,如果使用executor接口,那么線程創(chuàng)建的操作也會被隱藏起來
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class host{ private final helper helper = new helper(); private final executor executor; public host(executor executor){ this .executor = executor; } public void request( final int count, final char c){ system.out.println( "request方法開始了" ); executor.execute( new runnable() { @override public void run() { // todo auto-generated method stub helper.handle(count, c); } }); system.out.println( "request方法結(jié)束" ); } } |
使用java.util.concurrent.scheduledexecutorservice類創(chuàng)建,其可以實現(xiàn)調(diào)度運行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class host{ private final helper helper = new helper(); private final scheduledexecutorservice scheduledexecutorservice; public host(scheduledexecutorservice scheduledexecutorservice){ this .scheduledexecutorservice = scheduledexecutorservice; } public void request( final int count, final char c){ system.out.println( "request方法開始了" ); scheduledexecutorservice.schedule( new runnable() { @override public void run() { // todo auto-generated method stub helper.handle(count, c); } }, 3l, timeunit.seconds); system.out.println( "request方法結(jié)束" ); } } |
測試主函數(shù)入口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
scheduledexecutorservice scheduledexecutorservice = executors.newscheduledthreadpool( 5 ); host host = new host( scheduledexecutorservice ); try { host.request( 10 , 'a' ); host.request( 20 , 'b' ); host.request( 30 , 'c' ); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } finally { scheduledexecutorservice.shutdown(); system.out.println( "main end" ); } |
總結(jié)
client 角色調(diào)用host角色的request方法發(fā)來的請求,該請求的實際處理則交給helper的handle去執(zhí)行,然而,如果client直接從request中調(diào)用handle方法,那么直到實際操作結(jié)束之前,都無法從handle方法返回(request返回),這樣一來request的響應(yīng)性能就下降了,因此,host角色會啟動用于處理來自client角色請求的新線程,并讓該線程來調(diào)用handle,這樣一來發(fā)出請求的線程便可以立即從handle中返回。這就是thread-per-message模式。
原文鏈接:https://blog.csdn.net/qq_31350373/article/details/80454306